forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destructuring-bench.js
50 lines (45 loc) · 968 Bytes
/
destructuring-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
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
method: ['swap', 'destructure'],
n: [1e8]
});
function runSwapManual(n) {
var x, y, r;
bench.start();
for (var i = 0; i < n; i++) {
x = 1, y = 2;
r = x;
x = y;
y = r;
assert.strictEqual(x, 2);
assert.strictEqual(y, 1);
}
bench.end(n);
}
function runSwapDestructured(n) {
var x, y;
bench.start();
for (var i = 0; i < n; i++) {
x = 1, y = 2;
[x, y] = [y, x];
assert.strictEqual(x, 2);
assert.strictEqual(y, 1);
}
bench.end(n);
}
function main({ n, method }) {
switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'swap':
runSwapManual(n);
break;
case 'destructure':
runSwapDestructured(n);
break;
default:
throw new Error(`Unexpected method "${method}"`);
}
}