• 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: ['copy', 'rest', 'arguments'],
8  n: [1e8],
9});
10
11function copyArguments() {
12  const len = arguments.length;
13  const args = new Array(len);
14  for (let i = 0; i < len; i++)
15    args[i] = arguments[i];
16  assert.strictEqual(args[0], 1);
17  assert.strictEqual(args[1], 2);
18  assert.strictEqual(args[2], 'a');
19  assert.strictEqual(args[3], 'b');
20}
21
22function restArguments(...args) {
23  assert.strictEqual(args[0], 1);
24  assert.strictEqual(args[1], 2);
25  assert.strictEqual(args[2], 'a');
26  assert.strictEqual(args[3], 'b');
27}
28
29function useArguments() {
30  assert.strictEqual(arguments[0], 1);
31  assert.strictEqual(arguments[1], 2);
32  assert.strictEqual(arguments[2], 'a');
33  assert.strictEqual(arguments[3], 'b');
34}
35
36function runCopyArguments(n) {
37  for (let i = 0; i < n; i++)
38    copyArguments(1, 2, 'a', 'b');
39}
40
41function runRestArguments(n) {
42  for (let i = 0; i < n; i++)
43    restArguments(1, 2, 'a', 'b');
44}
45
46function runUseArguments(n) {
47  for (let i = 0; i < n; i++)
48    useArguments(1, 2, 'a', 'b');
49}
50
51function main({ n, method }) {
52  let fn;
53  switch (method) {
54    case 'copy':
55      fn = runCopyArguments;
56      break;
57    case 'rest':
58      fn = runRestArguments;
59      break;
60    case 'arguments':
61      fn = runUseArguments;
62      break;
63    default:
64      throw new Error(`Unexpected method "${method}"`);
65  }
66  bench.start();
67  fn(n);
68  bench.end(n);
69}
70