Skip to content

Commit e0157f3

Browse files
committed
Benchmark: add destructuring object
1 parent 66df5d1 commit e0157f3

File tree

1 file changed

+49
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)