• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4const assert = require('assert');
5
6const bench = common.createBenchmark(main, {
7  method: ['apply', 'spread', 'call-spread'],
8  count: [5, 10, 20],
9  context: ['context', 'null'],
10  rest: [0, 1],
11  n: [5e6],
12});
13
14function makeTest(count, rest) {
15  if (rest) {
16    return function test(...args) {
17      assert.strictEqual(count, args.length);
18    };
19  }
20  return function test() {
21    assert.strictEqual(count, arguments.length);
22  };
23}
24
25function main({ n, context, count, rest, method }) {
26  const ctx = context === 'context' ? {} : null;
27  let fn = makeTest(count, rest);
28  const args = new Array(count);
29
30  for (let i = 0; i < count; i++)
31    args[i] = i;
32
33  switch (method) {
34    case 'apply':
35      bench.start();
36      for (let i = 0; i < n; i++)
37        fn.apply(ctx, args);
38      bench.end(n);
39      break;
40    case 'spread':
41      if (ctx !== null)
42        fn = fn.bind(ctx);
43      bench.start();
44      for (let i = 0; i < n; i++)
45        fn(...args);
46      bench.end(n);
47      break;
48    case 'call-spread':
49      bench.start();
50      for (let i = 0; i < n; i++)
51        fn.call(ctx, ...args);
52      bench.end(n);
53      break;
54    default:
55      throw new Error(`Unexpected method "${method}"`);
56  }
57}
58