node-addon-layer
C API For writing Node modules
|
By writing a native module you are explicitly preventing that logic from being optimized by the JIT, it will only be as optimized as the target compiler provides. So things like function inlining and lookup caching can't be done by the JIT for logic defined in C.
There is a cost for a boundary jump between JavaScript and C, and it is more than the cost of a function call in pure JavaScript (espcially if the JIT inlined the call). But while the boundary cost is measurable, it is more expensive to actually mutate an object (i.e. set properties) in C compared to mutating the object in JavaScript.
To that end it is important to try and interact with your module using primitives, like:
That is, you should pass and return these primitves. You can pass and return an object, but don't modify the object in a hot path. If you want to construct an object in a hot path it is generally cheaper to also pass a function to create and return that object:
And then in JavaScript