mirror of
https://github.com/nodejs/node.git
synced 2024-11-24 03:07:54 +01:00
ca5f322d32
PR-URL: https://github.com/nodejs/node/pull/46370 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
51 lines
885 B
JavaScript
51 lines
885 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
const assert = require('assert');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
method: ['swap', 'destructure'],
|
|
n: [1e8],
|
|
});
|
|
|
|
function runSwapManual(n) {
|
|
let x, y, r;
|
|
bench.start();
|
|
for (let 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) {
|
|
let x, y;
|
|
bench.start();
|
|
for (let 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 'swap':
|
|
runSwapManual(n);
|
|
break;
|
|
case 'destructure':
|
|
runSwapDestructured(n);
|
|
break;
|
|
default:
|
|
throw new Error(`Unexpected method "${method}"`);
|
|
}
|
|
}
|