Why Should Write Node Modules In C++ If Possible.
2014-09-22 04:34
363 查看
The reason can't be more simple: the Need For Speed.
As a simple test between node-uuid and my own uuid implementations by boost, the result exposures the truth again: C++ is still faster than Javascript.
The time consumptions of 1M generations of uuid is almost 3s(node-uuid) to 0.3s(my boost uuid).
So, my advices is: if possible, write Node module by yourself with C++, the benefit is undeniable.
The following is the code of boost uuid:
node-uuid.h
test.js
As a simple test between node-uuid and my own uuid implementations by boost, the result exposures the truth again: C++ is still faster than Javascript.
The time consumptions of 1M generations of uuid is almost 3s(node-uuid) to 0.3s(my boost uuid).
So, my advices is: if possible, write Node module by yourself with C++, the benefit is undeniable.
The following is the code of boost uuid:
node-uuid.h
#ifndef NODE_UUID_H #define NODE_UUID_H using namespace v8; #define NODE_UUID_CLS_NAME "nodeUuid" namespace igame { namespace uuid { void initialize(Handle<Object> exports); } } // ns #endifnode-uuid.cpp
#include "node_uuid.hpp" // #include <boost/lexical_cast.hpp> namespace igame { namespace uuid { Persistent<Function> constructor; buid::random_generator uuid_generator; uint32_t uuid_count; Handle<Value> generate_uuid(const Arguments& args); Handle<Value> get_count(Local<String> property, const AccessorInfo& info); void initialize(Handle<Object> exports) { exports->Set(String::NewSymbol("next"), FunctionTemplate::New(generate_uuid)->GetFunction()); exports->SetAccessor(String::NewSymbol("count"), get_count, NULL); } Handle<Value> generate_uuid(const Arguments& args) { HandleScope scope; buid::uuid uid = uuid_generator(); uuid_count++; return scope.Close(String::New(buid::to_string(uid).c_str())); } Handle<Value> get_count(Local<String> property, const AccessorInfo& info) { HandleScope scope; return scope.Close(Integer::New(uuid_count)); } } // ns } // ns
test.js
var uuid = require('./build/Release/node-uuid'); console.log('uuid:', uuid.next()); console.log('uuid.count + ' uuid(s) has/have been generated.');
相关文章推荐
- When should we write our own assignment operator in C++?
- Why should you choose fstream classes for file I/O in C++?
- 如果你的软件事关重大,你就会用C++编写它(If your software matters, you will write it in C++)
- how to write it in C++?
- Programming Basic--why we need to add ' extern "C" ' in C++ program when calling the function which is complied by C complier?
- Why need two IF in singleton pattern in the multiple threads scenario
- Why does wide file-stream in C++ narrow written data by default?
- Which variables should I typecast when doing math operations in C/C++?
- Something about static Methods in C++ you should know
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- The Biggest Changes in C++11 (and Why You Should Care)
- Visual C++ Debugging: Why does program work in debug mode, but fail in release mode?
- The Biggest Changes in C++11 (and Why You Should Care)
- The Biggest Changes in C++11 (and Why You Should Care)(C++11语言标准主要变化)
- Why do we need extern “C”{ #include <foo.h> } in C++?
- Why are some things left undefined in C++?
- Question 27: Which of the following reasons describe why a destructor cannot throw an exception in C++?
- C++11的变化-The Biggest Changes in C++11 (and Why You Should Care)
- Do-While and if-else statements in C/C++ macros
- why I was failed in nodejs project