diff options
Diffstat (limited to 'benchmark/napi/function_args/index.js')
-rw-r--r-- | benchmark/napi/function_args/index.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/benchmark/napi/function_args/index.js b/benchmark/napi/function_args/index.js new file mode 100644 index 0000000000..c8f281a342 --- /dev/null +++ b/benchmark/napi/function_args/index.js @@ -0,0 +1,99 @@ +// show the difference between calling a V8 binding C++ function +// relative to a comparable N-API C++ function, +// in various types/numbers of arguments. +// Reports n of calls per second. +'use strict'; + +const common = require('../../common.js'); + +let v8; +let napi; + +try { + v8 = require('./build/Release/binding'); +} catch (err) { + // eslint-disable-next-line no-path-concat + console.error(__filename + ': V8 Binding failed to load'); + process.exit(0); +} + +try { + napi = require('./build/Release/napi_binding'); +} catch (err) { + // eslint-disable-next-line no-path-concat + console.error(__filename + ': NAPI-Binding failed to load'); + process.exit(0); +} + +const argsTypes = ['String', 'Number', 'Object', 'Array', 'Typedarray', + '10Numbers', '100Numbers', '1000Numbers']; + +const generateArgs = (argType) => { + let args = []; + + if (argType === 'String') { + args.push('The quick brown fox jumps over the lazy dog'); + } else if (argType === 'LongString') { + args.push(Buffer.alloc(32768, '42').toString()); + } else if (argType === 'Number') { + args.push(Math.floor(314158964 * Math.random())); + } else if (argType === 'Object') { + args.push({ + map: 'add', + operand: 10, + data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + reduce: 'add', + }); + } else if (argType === 'Array') { + const arr = []; + for (let i = 0; i < 50; ++i) { + arr.push(Math.random() * 10e9); + } + args.push(arr); + } else if (argType === 'Typedarray') { + const arr = new Uint32Array(1000); + for (let i = 0; i < 1000; ++i) { + arr[i] = Math.random() * 4294967296; + } + args.push(arr); + } else if (argType === '10Numbers') { + args.push(10); + for (let i = 0; i < 9; ++i) { + args = [...args, ...generateArgs('Number')]; + } + } else if (argType === '100Numbers') { + args.push(100); + for (let i = 0; i < 99; ++i) { + args = [...args, ...generateArgs('Number')]; + } + } else if (argType === '1000Numbers') { + args.push(1000); + for (let i = 0; i < 999; ++i) { + args = [...args, ...generateArgs('Number')]; + } + } + + return args; +}; + +const bench = common.createBenchmark(main, { + type: argsTypes, + engine: ['v8', 'napi'], + n: [1, 1e1, 1e2, 1e3, 1e4, 1e5], +}); + +function main({ n, engine, type }) { + const bindings = engine === 'v8' ? v8 : napi; + const methodName = 'callWith' + type; + const fn = bindings[methodName]; + + if (fn) { + const args = generateArgs(type); + + bench.start(); + for (var i = 0; i < n; i++) { + fn.apply(null, args); + } + bench.end(n); + } +} |