• 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: ['withoutdefaults', 'withdefaults'],
8  n: [1e8],
9});
10
11function oldStyleDefaults(x, y) {
12  x = x || 1;
13  y = y || 2;
14  assert.strictEqual(x, 1);
15  assert.strictEqual(y, 2);
16}
17
18function defaultParams(x = 1, y = 2) {
19  assert.strictEqual(x, 1);
20  assert.strictEqual(y, 2);
21}
22
23function runOldStyleDefaults(n) {
24  bench.start();
25  for (let i = 0; i < n; i++)
26    oldStyleDefaults();
27  bench.end(n);
28}
29
30function runDefaultParams(n) {
31  bench.start();
32  for (let i = 0; i < n; i++)
33    defaultParams();
34  bench.end(n);
35}
36
37function main({ n, method }) {
38  switch (method) {
39    case 'withoutdefaults':
40      runOldStyleDefaults(n);
41      break;
42    case 'withdefaults':
43      runDefaultParams(n);
44      break;
45    default:
46      throw new Error(`Unexpected method "${method}"`);
47  }
48}
49