forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultparams-bench.js
More file actions
48 lines (41 loc) · 916 Bytes
/
defaultparams-bench.js
File metadata and controls
48 lines (41 loc) · 916 Bytes
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
'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 ||= 1;
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 (let i = 0; i < n; i++)
oldStyleDefaults();
bench.end(n);
}
function runDefaultParams(n) {
bench.start();
for (let i = 0; i < n; i++)
defaultParams();
bench.end(n);
}
function main({ n, method }) {
switch (method) {
case 'withoutdefaults':
runOldStyleDefaults(n);
break;
case 'withdefaults':
runDefaultParams(n);
break;
default:
throw new Error(`Unexpected method "${method}"`);
}
}