Why V8 in Node.JS is faster than in my native C++ addon? Why V8 in Node.JS is faster than in my native C++ addon? node.js node.js

Why V8 in Node.JS is faster than in my native C++ addon?


In the C++ version all variables declared in the script source (result, primeNumberCounter, i, j, isPrime, start, end, time) are global because script's top level scope is global scope.

For optimizing compiler it is easy to allocate local variables into machine registers (or spill slots on stack) and keep track of their type. Working with global variables on the other hand requires constant memory accesses and type checks because V8 does not (currently) perform a register promotion optimization.

If you wrap the source into an immediately called function difference should go away.