How do native addons affect node.js program?

60 views Asked by At

I have such codes:

const test = require('./build/Release/test');
console.time('js');
console.log(f(0, 0, 1000, 1000, 50.6));
console.timeEnd('js');
console.time('c++');
console.log(test.sum(0, 0, 1000, 1000, 50.6));
console.timeEnd('c++');
const test = require('./build/Release/test');
console.time('c++');
console.log(test.sum(0, 0, 1000, 1000, 50.6));
console.timeEnd('c++');
console.time('js');
console.log(f(0, 0, 1000, 1000, 50.6));
console.timeEnd('js');

They work correctly, give same results, everything is great, but the only problem is that in the first code JS f() takes ~15ms, but in the second example it takes ~3ms. And this results are stable (when I start the program more than once, it gives nearly same results). How can it be? Why does performance of the function on JS depends on calling the independent function from a native addon?

P.S. Here is the console from two runs of the program:

{"x": -151.932697, "y": 235.000000}
js: 17.064ms
{"x": -151.932697, "y": 235.000000}
c++: 7.908ms
{"x": -151.932697, "y": 235.000000}
c++: 8.191ms
{"x": -151.932697, "y": 235.000000}
js: 2.578ms
0

There are 0 answers