Konstantin Käfer

kkaefer

node.js extensions
with C++ and V8

C++ is faster than JavaScript. Is it?

If possible, write it in JavaScript.
JS ⟷ C++ boundary is slow to cross

function() { return Math.floor(133.7 / Math.PI); }
native JS  ~83,333,333 calls/s
V8 return ~13,333,333 calls/s
V8 async ~1,745,200 calls/s
thread pool ~83,682 calls/s (0.1%)

Why C++?

The bare minimum

modulename.cpp

The bare minimum

wscript

https://github.com/
kkaefer/node-cpp-modules

Compiling

$  node-waf configure 
Setting srcdir to               : /.../minimum
Setting blddir to               : /.../minimum/build
Checking for program g++ or c++ : /usr/bin/g++
Checking for program cpp        : /usr/bin/cpp
Checking for program ar         : /usr/bin/ar
Checking for program ranlib     : /usr/bin/ranlib
Checking for g++                : ok
Checking for node path          : not found
Checking for node prefix        : ok /usr/local
'configure' finished successfully (0.057s)
$  node-waf build 
Waf: Entering directory `/.../minimum/build'
[1/2] cxx: modulename.cpp -> build/Release/modulename_1.o
[2/2] cxx_link: build/Release/modulename_1.o ->
          build/Release/modulename.node
Waf: Leaving directory `/.../minimum/build'
'build' finished successfully (0.539s)
$  

Requiring from JavaScript

$ node
>  require('./build/Release/modulename.node'); 
{}
>  

Develop with
$ CXX=clang node-waf configure
Also test with GCC

Defining functions

modulename.cpp

Exporting functions

modulename.cpp
$ node
>  var mod = require('./build/Release/modulename.node'); 
{ theAnswer: [Function] }
>  mod.theAnswer(); 
42
>  

Function arguments

modulename.cpp

Callback functions

modulename.cpp

The thread pool



Passing data to the thread pool

Calling asynchronously

Worker function

After function

Wrap callbacks in TryCatch

API design

node-sqlite3 API

node-blend API

More documentation

kkaefer