forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefaultparams-bench.js
54 lines (44 loc) · 969 Bytes
/
defaultparams-bench.js
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
51
52
53
54
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
method: ['withoutdefaults', 'withdefaults'],
millions: [100]
});
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) {
var i = 0;
bench.start();
for (; i < n; i++)
oldStyleDefaults();
bench.end(n / 1e6);
}
function runDefaultParams(n) {
var i = 0;
bench.start();
for (; i < n; i++)
defaultParams();
bench.end(n / 1e6);
}
function main(conf) {
const n = +conf.millions * 1e6;
switch (conf.method) {
case 'withoutdefaults':
runOldStyleDefaults(n);
break;
case 'withdefaults':
runDefaultParams(n);
break;
default:
throw new Error('Unexpected method');
}
}