mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
193468667b
This rule enforces new lines around variable declarations. It is configured to spot only variables that are initialized. PR-URL: https://github.com/nodejs/node/pull/11462 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
51 lines
914 B
JavaScript
51 lines
914 B
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
const assert = require('assert');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
method: ['swap', 'destructure'],
|
|
millions: [100]
|
|
});
|
|
|
|
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 / 1e6);
|
|
}
|
|
|
|
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 / 1e6);
|
|
}
|
|
|
|
function main(conf) {
|
|
const n = +conf.millions * 1e6;
|
|
|
|
switch (conf.method) {
|
|
case 'swap':
|
|
runSwapManual(n);
|
|
break;
|
|
case 'destructure':
|
|
runSwapDestructured(n);
|
|
break;
|
|
default:
|
|
throw new Error('Unexpected method');
|
|
}
|
|
}
|