• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 const assert = require('assert');
8 const 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 
14 let binding;
15 try {
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 }
21 const cxx = binding.hello;
22 
23 let napi_binding;
24 try {
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 }
30 const napi = napi_binding.hello;
31 
32 let c = 0;
33 function js() {
34   return c++;
35 }
36 
37 assert(js() === cxx());
38 
39 const bench = common.createBenchmark(main, {
40   type: ['js', 'cxx', 'napi'],
41   n: [1e6, 1e7, 5e7]
42 });
43 
44 function 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