summaryrefslogtreecommitdiff
path: root/deps/node/benchmark/es/defaultparams-bench.js
blob: c568c12ae03fda7e65fc5f0546b2e6f453c17ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';

const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
  method: ['withoutdefaults', 'withdefaults'],
  n: [1e8]
});

function oldStyleDefaults(x, y) {
  x = x || 1;
  y = y || 2;
  assert.strictEqual(x, 1);
  assert.strictEqual(y, 2);
}

function defaultParams(x = 1, y = 2) {
  assert.strictEqual(x, 1);
  assert.strictEqual(y, 2);
}

function runOldStyleDefaults(n) {
  bench.start();
  for (var i = 0; i < n; i++)
    oldStyleDefaults();
  bench.end(n);
}

function runDefaultParams(n) {
  bench.start();
  for (var i = 0; i < n; i++)
    defaultParams();
  bench.end(n);
}

function main({ n, method }) {
  switch (method) {
    case '':
      // Empty string falls through to next line as default, mostly for tests.
    case 'withoutdefaults':
      runOldStyleDefaults(n);
      break;
    case 'withdefaults':
      runDefaultParams(n);
      break;
    default:
      throw new Error(`Unexpected method "${method}"`);
  }
}