1// Show the difference between calling a short js function 2// relative to a comparable C++ function. 3// Reports n of calls per second. 4// Note that JS speed goes up, while cxx speed stays about the same. 5'use strict'; 6 7const assert = require('assert'); 8const common = require('../../common.js'); 9 10// This fails when we try to open with a different version of node, 11// which is quite common for benchmarks. so in that case, just 12// abort quietly. 13 14let binding; 15try { 16 binding = require(`./build/${common.buildType}/binding`); 17} catch { 18 console.error('misc/function_call.js Binding failed to load'); 19 process.exit(0); 20} 21const cxx = binding.hello; 22 23let napi_binding; 24try { 25 napi_binding = require(`./build/${common.buildType}/napi_binding`); 26} catch { 27 console.error('misc/function_call/index.js NAPI-Binding failed to load'); 28 process.exit(0); 29} 30const napi = napi_binding.hello; 31 32let c = 0; 33function js() { 34 return c++; 35} 36 37assert(js() === cxx()); 38 39const bench = common.createBenchmark(main, { 40 type: ['js', 'cxx', 'napi'], 41 n: [1e6, 1e7, 5e7], 42}); 43 44function main({ n, type }) { 45 const fn = type === 'cxx' ? cxx : type === 'napi' ? napi : js; 46 bench.start(); 47 for (let i = 0; i < n; i++) { 48 fn(); 49 } 50 bench.end(n); 51} 52