Skip to content

Commit 451f4fc

Browse files
committed
benchmarks: add microbenchmarks for new ES6 features
Adds new microbenchmarks for destructuring, rest params and default params. PR-URL: #6222 Reviewed-By: Brian White <[email protected]>
1 parent 962c1e1 commit 451f4fc

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

benchmark/es/defaultparams-bench.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
method: ['withoutdefaults', 'withdefaults'],
8+
millions: [100]
9+
});
10+
11+
function oldStyleDefaults(x, y) {
12+
x = x || 1;
13+
y = y || 2;
14+
assert.strictEqual(x, 1);
15+
assert.strictEqual(y, 2);
16+
}
17+
18+
function defaultParams(x = 1, y = 2) {
19+
assert.strictEqual(x, 1);
20+
assert.strictEqual(y, 2);
21+
}
22+
23+
function runOldStyleDefaults(n) {
24+
25+
common.v8ForceOptimization(oldStyleDefaults);
26+
27+
var i = 0;
28+
bench.start();
29+
for (; i < n; i++)
30+
oldStyleDefaults();
31+
bench.end(n / 1e6);
32+
}
33+
34+
function runDefaultParams(n) {
35+
36+
common.v8ForceOptimization(defaultParams);
37+
38+
var i = 0;
39+
bench.start();
40+
for (; i < n; i++)
41+
defaultParams();
42+
bench.end(n / 1e6);
43+
}
44+
45+
function main(conf) {
46+
const n = +conf.millions * 1e6;
47+
48+
switch (conf.method) {
49+
case 'withoutdefaults':
50+
runOldStyleDefaults(n);
51+
break;
52+
case 'withdefaults':
53+
runDefaultParams(n);
54+
break;
55+
default:
56+
throw new Error('Unexpected method');
57+
}
58+
}

benchmark/es/destructuring-bench.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
method: ['swap', 'destructure'],
8+
millions: [100]
9+
});
10+
11+
function runSwapManual(n) {
12+
var i = 0, x, y, r;
13+
bench.start();
14+
for (; i < n; i++) {
15+
x = 1, y = 2;
16+
r = x;
17+
x = y;
18+
y = r;
19+
assert.strictEqual(x, 2);
20+
assert.strictEqual(y, 1);
21+
}
22+
bench.end(n / 1e6);
23+
}
24+
25+
function runSwapDestructured(n) {
26+
var i = 0, x, y;
27+
bench.start();
28+
for (; i < n; i++) {
29+
x = 1, y = 2;
30+
[x, y] = [y, x];
31+
assert.strictEqual(x, 2);
32+
assert.strictEqual(y, 1);
33+
}
34+
bench.end(n / 1e6);
35+
}
36+
37+
function main(conf) {
38+
const n = +conf.millions * 1e6;
39+
40+
switch (conf.method) {
41+
case 'swap':
42+
runSwapManual(n);
43+
break;
44+
case 'destructure':
45+
runSwapDestructured(n);
46+
break;
47+
default:
48+
throw new Error('Unexpected method');
49+
}
50+
}

benchmark/es/restparams-bench.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
method: ['copy', 'rest', 'arguments'],
8+
millions: [100]
9+
});
10+
11+
function copyArguments() {
12+
var len = arguments.length;
13+
var args = new Array(len);
14+
for (var 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+
22+
function 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+
29+
function 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+
36+
function runCopyArguments(n) {
37+
38+
common.v8ForceOptimization(copyArguments, 1, 2, 'a', 'b');
39+
40+
var i = 0;
41+
bench.start();
42+
for (; i < n; i++)
43+
copyArguments(1, 2, 'a', 'b');
44+
bench.end(n / 1e6);
45+
}
46+
47+
function runRestArguments(n) {
48+
49+
common.v8ForceOptimization(restArguments, 1, 2, 'a', 'b');
50+
51+
var i = 0;
52+
bench.start();
53+
for (; i < n; i++)
54+
restArguments(1, 2, 'a', 'b');
55+
bench.end(n / 1e6);
56+
}
57+
58+
function runUseArguments(n) {
59+
60+
common.v8ForceOptimization(useArguments, 1, 2, 'a', 'b');
61+
62+
var i = 0;
63+
bench.start();
64+
for (; i < n; i++)
65+
useArguments(1, 2, 'a', 'b');
66+
bench.end(n / 1e6);
67+
}
68+
69+
function main(conf) {
70+
const n = +conf.millions * 1e6;
71+
72+
switch (conf.method) {
73+
case 'copy':
74+
runCopyArguments(n);
75+
break;
76+
case 'rest':
77+
runRestArguments(n);
78+
break;
79+
case 'arguments':
80+
runUseArguments(n);
81+
break;
82+
default:
83+
throw new Error('Unexpected method');
84+
}
85+
}

0 commit comments

Comments
 (0)