diff options
author | James M Snell <jasnell@gmail.com> | 2017-02-07 23:42:04 +0300 |
---|---|---|
committer | Italo A. Casas <me@italoacasas.com> | 2017-02-14 19:52:29 +0300 |
commit | 599c9472762433f4ee6a003aa7ee41dff172055c (patch) | |
tree | 67b71a52a2b69fa01ec3747ed5b1cf9284f37974 /benchmark | |
parent | a5e8176feecd4c2da0a5e330b38df3962af1b5d5 (diff) |
benchmarks: add spread operator benchmark
Useful for comparing spread operator performance over time.
PR-URL: https://github.com/nodejs/node/pull/11227
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/es/spread-bench.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/benchmark/es/spread-bench.js b/benchmark/es/spread-bench.js new file mode 100644 index 00000000000..a9416ad90ef --- /dev/null +++ b/benchmark/es/spread-bench.js @@ -0,0 +1,59 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + method: ['apply', 'spread', 'call-spread'], + count: [5, 10, 20], + context: ['context', 'null'], + rest: [0, 1], + millions: [5] +}); + +function makeTest(count, rest) { + if (rest) { + return function test(...args) { + assert.strictEqual(count, args.length); + }; + } else { + return function test() { + assert.strictEqual(count, arguments.length); + }; + } +} + +function main(conf) { + const n = +conf.millions * 1e6; + const ctx = conf.context === 'context' ? {} : null; + var fn = makeTest(conf.count, conf.rest); + const args = new Array(conf.count); + var i; + for (i = 0; i < conf.count; i++) + args[i] = i; + + switch (conf.method) { + case 'apply': + bench.start(); + for (i = 0; i < n; i++) + fn.apply(ctx, args); + bench.end(n / 1e6); + break; + case 'spread': + if (ctx !== null) + fn = fn.bind(ctx); + bench.start(); + for (i = 0; i < n; i++) + fn(...args); + bench.end(n / 1e6); + break; + case 'call-spread': + bench.start(); + for (i = 0; i < n; i++) + fn.call(ctx, ...args); + bench.end(n / 1e6); + break; + default: + throw new Error('Unexpected method'); + } +} |