Skip to content

Commit

Permalink
benchmark: use let and const instead of var
Browse files Browse the repository at this point in the history
Use let and const in domain, es, events, fixtures, fs, http,
http2 and misc.

PR-URL: #31518
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
dnlup authored and codebytere committed Mar 30, 2020
1 parent 22a37d6 commit 2973c1f
Show file tree
Hide file tree
Showing 35 changed files with 105 additions and 111 deletions.
2 changes: 1 addition & 1 deletion benchmark/domain/domain-fn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function main({ n, args }) {
bench.start();

bdomain.enter();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
if (myArguments.length >= 2) {
const args = Array.prototype.slice.call(myArguments, 1);
fn.apply(this, args);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/es/defaultparams-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ function defaultParams(x = 1, y = 2) {

function runOldStyleDefaults(n) {
bench.start();
for (var i = 0; i < n; i++)
for (let i = 0; i < n; i++)
oldStyleDefaults();
bench.end(n);
}

function runDefaultParams(n) {
bench.start();
for (var i = 0; i < n; i++)
for (let i = 0; i < n; i++)
defaultParams();
bench.end(n);
}
Expand Down
8 changes: 4 additions & 4 deletions benchmark/es/destructuring-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const bench = common.createBenchmark(main, {
});

function runSwapManual(n) {
var x, y, r;
let x, y, r;
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
x = 1, y = 2;
r = x;
x = y;
Expand All @@ -23,9 +23,9 @@ function runSwapManual(n) {
}

function runSwapDestructured(n) {
var x, y;
let x, y;
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
x = 1, y = 2;
[x, y] = [y, x];
assert.strictEqual(x, 2);
Expand Down
6 changes: 2 additions & 4 deletions benchmark/es/destructuring-object-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ const bench = common.createBenchmark(main, {
});

function runNormal(n) {
var i = 0;
const o = { x: 0, y: 1 };
bench.start();
for (; i < n; i++) {
for (let i = 0; i < n; i++) {
/* eslint-disable no-unused-vars */
const x = o.x;
const y = o.y;
Expand All @@ -22,10 +21,9 @@ function runNormal(n) {
}

function runDestructured(n) {
var i = 0;
const o = { x: 0, y: 1 };
bench.start();
for (; i < n; i++) {
for (let i = 0; i < n; i++) {
/* eslint-disable no-unused-vars */
const { x, y, r = 2 } = o;
/* eslint-enable no-unused-vars */
Expand Down
16 changes: 8 additions & 8 deletions benchmark/es/foreach-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const bench = common.createBenchmark(main, {

function useFor(n, items, count) {
bench.start();
for (var i = 0; i < n; i++) {
for (var j = 0; j < count; j++) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < count; j++) {
// eslint-disable-next-line no-unused-vars
const item = items[j];
}
Expand All @@ -21,7 +21,7 @@ function useFor(n, items, count) {

function useForOf(n, items) {
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
// eslint-disable-next-line no-unused-vars
for (const item of items) {}
}
Expand All @@ -30,8 +30,8 @@ function useForOf(n, items) {

function useForIn(n, items) {
bench.start();
for (var i = 0; i < n; i++) {
for (var j in items) {
for (let i = 0; i < n; i++) {
for (const j in items) {
// eslint-disable-next-line no-unused-vars
const item = items[j];
}
Expand All @@ -41,16 +41,16 @@ function useForIn(n, items) {

function useForEach(n, items) {
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
items.forEach((item) => {});
}
bench.end(n);
}

function main({ n, count, method }) {
const items = new Array(count);
var fn;
for (var i = 0; i < count; i++)
let fn;
for (let i = 0; i < count; i++)
items[i] = i;

switch (method) {
Expand Down
12 changes: 6 additions & 6 deletions benchmark/es/map-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const bench = common.createBenchmark(main, {
function runObject(n) {
const m = {};
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
Expand All @@ -27,7 +27,7 @@ function runObject(n) {
function runNullProtoObject(n) {
const m = Object.create(null);
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
Expand All @@ -40,7 +40,7 @@ function runNullProtoObject(n) {
function runNullProtoLiteralObject(n) {
const m = { __proto__: null };
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
Expand All @@ -56,7 +56,7 @@ StorageObject.prototype = Object.create(null);
function runStorageObject(n) {
const m = new StorageObject();
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
m[`i${i}`] = i;
m[`s${i}`] = String(i);
assert.strictEqual(String(m[`i${i}`]), m[`s${i}`]);
Expand All @@ -79,7 +79,7 @@ function fakeMap() {
function runFakeMap(n) {
const m = fakeMap();
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
m.set(`i${i}`, i);
m.set(`s${i}`, String(i));
assert.strictEqual(String(m.get(`i${i}`)), m.get(`s${i}`));
Expand All @@ -92,7 +92,7 @@ function runFakeMap(n) {
function runMap(n) {
const m = new Map();
bench.start();
for (var i = 0; i < n; i++) {
for (let i = 0; i < n; i++) {
m.set(`i${i}`, i);
m.set(`s${i}`, String(i));
assert.strictEqual(String(m.get(`i${i}`)), m.get(`s${i}`));
Expand Down
10 changes: 5 additions & 5 deletions benchmark/es/restparams-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const bench = common.createBenchmark(main, {
function copyArguments() {
const len = arguments.length;
const args = new Array(len);
for (var i = 0; i < len; i++)
for (let i = 0; i < len; i++)
args[i] = arguments[i];
assert.strictEqual(args[0], 1);
assert.strictEqual(args[1], 2);
Expand All @@ -34,22 +34,22 @@ function useArguments() {
}

function runCopyArguments(n) {
for (var i = 0; i < n; i++)
for (let i = 0; i < n; i++)
copyArguments(1, 2, 'a', 'b');
}

function runRestArguments(n) {
for (var i = 0; i < n; i++)
for (let i = 0; i < n; i++)
restArguments(1, 2, 'a', 'b');
}

function runUseArguments(n) {
for (var i = 0; i < n; i++)
for (let i = 0; i < n; i++)
useArguments(1, 2, 'a', 'b');
}

function main({ n, method }) {
var fn;
let fn;
switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
Expand Down
7 changes: 3 additions & 4 deletions benchmark/es/spread-assign.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,25 @@ function main({ n, context, count, rest, method }) {
src[`p${n}`] = n;

let obj; // eslint-disable-line no-unused-vars
let i;

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case '_extend':
bench.start();
for (i = 0; i < n; i++)
for (let i = 0; i < n; i++)
obj = util._extend({}, src);
bench.end(n);
break;
case 'assign':
bench.start();
for (i = 0; i < n; i++)
for (let i = 0; i < n; i++)
obj = Object.assign({}, src);
bench.end(n);
break;
case 'spread':
bench.start();
for (i = 0; i < n; i++)
for (let i = 0; i < n; i++)
obj = { ...src };
bench.end(n);
break;
Expand Down
12 changes: 6 additions & 6 deletions benchmark/es/spread-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ function makeTest(count, rest) {

function main({ n, context, count, rest, method }) {
const ctx = context === 'context' ? {} : null;
var fn = makeTest(count, rest);
let fn = makeTest(count, rest);
const args = new Array(count);
var i;
for (i = 0; i < count; i++)

for (let i = 0; i < count; i++)
args[i] = i;

switch (method) {
case '':
// Empty string falls through to next line as default, mostly for tests.
case 'apply':
bench.start();
for (i = 0; i < n; i++)
for (let i = 0; i < n; i++)
fn.apply(ctx, args);
bench.end(n);
break;
case 'spread':
if (ctx !== null)
fn = fn.bind(ctx);
bench.start();
for (i = 0; i < n; i++)
for (let i = 0; i < n; i++)
fn(...args);
bench.end(n);
break;
case 'call-spread':
bench.start();
for (i = 0; i < n; i++)
for (let i = 0; i < n; i++)
fn.call(ctx, ...args);
bench.end(n);
break;
Expand Down
9 changes: 4 additions & 5 deletions benchmark/events/ee-add-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ function main({ n }) {
const ee = new events.EventEmitter();
const listeners = [];

var k;
for (k = 0; k < 10; k += 1)
for (let k = 0; k < 10; k += 1)
listeners.push(() => {});

bench.start();
for (var i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
for (k = listeners.length; --k >= 0; /* empty */) {
for (let k = listeners.length; --k >= 0; /* empty */) {
ee.on(dummy, listeners[k]);
}
for (k = listeners.length; --k >= 0; /* empty */) {
for (let k = listeners.length; --k >= 0; /* empty */) {
ee.removeListener(dummy, listeners[k]);
}
}
Expand Down
11 changes: 5 additions & 6 deletions benchmark/events/ee-emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,34 @@ const bench = common.createBenchmark(main, {
function main({ n, argc, listeners }) {
const ee = new EventEmitter();

for (var k = 0; k < listeners; k += 1)
for (let k = 0; k < listeners; k += 1)
ee.on('dummy', () => {});

var i;
switch (argc) {
case 2:
bench.start();
for (i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
ee.emit('dummy', true, 5);
}
bench.end(n);
break;
case 4:
bench.start();
for (i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
ee.emit('dummy', true, 5, 10, false);
}
bench.end(n);
break;
case 10:
bench.start();
for (i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
ee.emit('dummy', true, 5, 10, false, 5, 'string', true, false, 11, 20);
}
bench.end(n);
break;
default:
bench.start();
for (i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
ee.emit('dummy');
}
bench.end(n);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/events/ee-listener-count-on-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const bench = common.createBenchmark(main, { n: [5e7] });
function main({ n }) {
const ee = new EventEmitter();

for (var k = 0; k < 5; k += 1) {
for (let k = 0; k < 5; k += 1) {
ee.on('dummy0', () => {});
ee.on('dummy1', () => {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.listenerCount(dummy);
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/events/ee-listeners-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function main({ n }) {
const ee = new EventEmitter();
ee.setMaxListeners(101);

for (var k = 0; k < 50; k += 1) {
for (let k = 0; k < 50; k += 1) {
ee.on('dummy0', () => {});
ee.on('dummy1', () => {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.listeners(dummy);
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/events/ee-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const bench = common.createBenchmark(main, { n: [5e6] });
function main({ n }) {
const ee = new EventEmitter();

for (var k = 0; k < 5; k += 1) {
for (let k = 0; k < 5; k += 1) {
ee.on('dummy0', () => {});
ee.on('dummy1', () => {});
}

bench.start();
for (var i = 0; i < n; i += 1) {
for (let i = 0; i < n; i += 1) {
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1';
ee.listeners(dummy);
}
Expand Down
Loading

0 comments on commit 2973c1f

Please sign in to comment.