benchmark: use let instead of var in assert

PR-URL: https://github.com/nodejs/node/pull/30450
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
dnlup 2019-11-12 19:56:03 +01:00 committed by Gireesh Punathil
parent 3000c2528c
commit d1ce04c592
9 changed files with 58 additions and 47 deletions

View File

@ -27,7 +27,7 @@ function main({ len, n, method, strict }) {
const value2 = method.includes('not') ? expectedWrong : expected; const value2 = method.includes('not') ? expectedWrong : expected;
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
fn(actual, value2); fn(actual, value2);
} }
bench.end(n); bench.end(n);

View File

@ -24,7 +24,7 @@ function benchmark(method, n, values, values2) {
const deepCopy = JSON.parse(JSON.stringify(values2 ? values2 : values)); const deepCopy = JSON.parse(JSON.stringify(values2 ? values2 : values));
const expected = new Map(deepCopy); const expected = new Map(deepCopy);
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
method(actual, expected); method(actual, expected);
} }
bench.end(n); bench.end(n);
@ -32,41 +32,50 @@ function benchmark(method, n, values, values2) {
function main({ n, len, method, strict }) { function main({ n, len, method, strict }) {
const array = Array(len).fill(1); const array = Array(len).fill(1);
var values, values2;
switch (method) { switch (method) {
case '': case '':
// Empty string falls through to next line as default, mostly for tests. // Empty string falls through to next line as default, mostly for tests.
case 'deepEqual_primitiveOnly': case 'deepEqual_primitiveOnly': {
values = array.map((_, i) => [`str_${i}`, 123]); const values = array.map((_, i) => [`str_${i}`, 123]);
benchmark(strict ? deepStrictEqual : deepEqual, n, values); benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break; break;
case 'deepEqual_objectOnly': }
values = array.map((_, i) => [[`str_${i}`, 1], 123]); case 'deepEqual_objectOnly': {
const values = array.map((_, i) => [[`str_${i}`, 1], 123]);
benchmark(strict ? deepStrictEqual : deepEqual, n, values); benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break; break;
case 'deepEqual_mixed': }
values = array.map((_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]); case 'deepEqual_mixed': {
const values = array.map(
(_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]
);
benchmark(strict ? deepStrictEqual : deepEqual, n, values); benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break; break;
case 'notDeepEqual_primitiveOnly': }
values = array.map((_, i) => [`str_${i}`, 123]); case 'notDeepEqual_primitiveOnly': {
values2 = values.slice(0); const values = array.map((_, i) => [`str_${i}`, 123]);
const values2 = values.slice(0);
values2[Math.floor(len / 2)] = ['w00t', 123]; values2[Math.floor(len / 2)] = ['w00t', 123];
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2); benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break; break;
case 'notDeepEqual_objectOnly': }
values = array.map((_, i) => [[`str_${i}`, 1], 123]); case 'notDeepEqual_objectOnly': {
values2 = values.slice(0); const values = array.map((_, i) => [[`str_${i}`, 1], 123]);
const values2 = values.slice(0);
values2[Math.floor(len / 2)] = [['w00t'], 123]; values2[Math.floor(len / 2)] = [['w00t'], 123];
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2); benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break; break;
case 'notDeepEqual_mixed': }
values = array.map((_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]); case 'notDeepEqual_mixed': {
values2 = values.slice(0); const values = array.map(
(_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]
);
const values2 = values.slice(0);
values2[0] = ['w00t', 123]; values2[0] = ['w00t', 123];
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2); benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break; break;
}
default: default:
throw new Error(`Unsupported method ${method}`); throw new Error(`Unsupported method ${method}`);
} }

View File

@ -42,7 +42,7 @@ function main({ size, n, method, strict }) {
const value2 = method.includes('not') ? expectedWrong : expected; const value2 = method.includes('not') ? expectedWrong : expected;
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
fn(actual, value2); fn(actual, value2);
} }
bench.end(n); bench.end(n);

View File

@ -26,7 +26,7 @@ const bench = common.createBenchmark(main, {
function run(fn, n, actual, expected) { function run(fn, n, actual, expected) {
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
fn(actual, expected); fn(actual, expected);
} }
bench.end(n); bench.end(n);
@ -38,7 +38,7 @@ function main({ n, len, primitive, method, strict }) {
const expected = []; const expected = [];
const expectedWrong = []; const expectedWrong = [];
for (var x = 0; x < len; x++) { for (let x = 0; x < len; x++) {
actual.push(prim); actual.push(prim);
expected.push(prim); expected.push(prim);
expectedWrong.push(prim); expectedWrong.push(prim);

View File

@ -31,7 +31,7 @@ function main({ n, primitive, method, strict }) {
const value2 = method.includes('not') ? expectedWrong : expected; const value2 = method.includes('not') ? expectedWrong : expected;
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
fn([actual], [value2]); fn([actual], [value2]);
} }
bench.end(n); bench.end(n);

View File

@ -24,7 +24,7 @@ function benchmark(method, n, values, values2) {
const deepCopy = JSON.parse(JSON.stringify(values2 ? values2 : values)); const deepCopy = JSON.parse(JSON.stringify(values2 ? values2 : values));
const expected = new Set(deepCopy); const expected = new Set(deepCopy);
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
method(actual, expected); method(actual, expected);
} }
bench.end(n); bench.end(n);
@ -33,45 +33,49 @@ function benchmark(method, n, values, values2) {
function main({ n, len, method, strict }) { function main({ n, len, method, strict }) {
const array = Array(len).fill(1); const array = Array(len).fill(1);
var values, values2;
switch (method) { switch (method) {
case '': case '':
// Empty string falls through to next line as default, mostly for tests. // Empty string falls through to next line as default, mostly for tests.
case 'deepEqual_primitiveOnly': case 'deepEqual_primitiveOnly': {
values = array.map((_, i) => `str_${i}`); const values = array.map((_, i) => `str_${i}`);
benchmark(strict ? deepStrictEqual : deepEqual, n, values); benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break; break;
case 'deepEqual_objectOnly': }
values = array.map((_, i) => [`str_${i}`, null]); case 'deepEqual_objectOnly': {
const values = array.map((_, i) => [`str_${i}`, null]);
benchmark(strict ? deepStrictEqual : deepEqual, n, values); benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break; break;
case 'deepEqual_mixed': }
values = array.map((_, i) => { case 'deepEqual_mixed': {
const values = array.map((_, i) => {
return i % 2 ? [`str_${i}`, null] : `str_${i}`; return i % 2 ? [`str_${i}`, null] : `str_${i}`;
}); });
benchmark(strict ? deepStrictEqual : deepEqual, n, values); benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break; break;
case 'notDeepEqual_primitiveOnly': }
values = array.map((_, i) => `str_${i}`); case 'notDeepEqual_primitiveOnly': {
values2 = values.slice(0); const values = array.map((_, i) => `str_${i}`);
const values2 = values.slice(0);
values2[Math.floor(len / 2)] = 'w00t'; values2[Math.floor(len / 2)] = 'w00t';
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2); benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break; break;
case 'notDeepEqual_objectOnly': }
values = array.map((_, i) => [`str_${i}`, null]); case 'notDeepEqual_objectOnly': {
values2 = values.slice(0); const values = array.map((_, i) => [`str_${i}`, null]);
const values2 = values.slice(0);
values2[Math.floor(len / 2)] = ['w00t']; values2[Math.floor(len / 2)] = ['w00t'];
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2); benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break; break;
case 'notDeepEqual_mixed': }
values = array.map((_, i) => { case 'notDeepEqual_mixed': {
const values = array.map((_, i) => {
return i % 2 ? [`str_${i}`, null] : `str_${i}`; return i % 2 ? [`str_${i}`, null] : `str_${i}`;
}); });
values2 = values.slice(); const values2 = values.slice();
values2[0] = 'w00t'; values2[0] = 'w00t';
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2); benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break; break;
}
default: default:
throw new Error(`Unsupported method "${method}"`); throw new Error(`Unsupported method "${method}"`);
} }

View File

@ -36,7 +36,7 @@ function main({ type, n, len, method, strict }) {
const value2 = method.includes('not') ? expectedWrong : expected; const value2 = method.includes('not') ? expectedWrong : expected;
bench.start(); bench.start();
for (var i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
actual[0] = i; actual[0] = i;
value2[0] = i; value2[0] = i;
fn(actual, value2); fn(actual, value2);

View File

@ -6,9 +6,8 @@ const assert = require('assert');
const bench = common.createBenchmark(main, { n: [1e5] }); const bench = common.createBenchmark(main, { n: [1e5] });
function main({ n }) { function main({ n }) {
var i;
bench.start(); bench.start();
for (i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
if (i % 2 === 0) if (i % 2 === 0)
assert(true); assert(true);
else else

View File

@ -13,28 +13,27 @@ function main({ n, method }) {
const doNotThrowError = () => { return 'foobar'; }; const doNotThrowError = () => { return 'foobar'; };
const regExp = /foobar/; const regExp = /foobar/;
const message = 'failure'; const message = 'failure';
var i;
switch (method) { switch (method) {
case '': case '':
// Empty string falls through to next line as default, mostly for tests. // Empty string falls through to next line as default, mostly for tests.
case 'doesNotThrow': case 'doesNotThrow':
bench.start(); bench.start();
for (i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
doesNotThrow(doNotThrowError); doesNotThrow(doNotThrowError);
} }
bench.end(n); bench.end(n);
break; break;
case 'throws_TypeError': case 'throws_TypeError':
bench.start(); bench.start();
for (i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
throws(throwError, TypeError, message); throws(throwError, TypeError, message);
} }
bench.end(n); bench.end(n);
break; break;
case 'throws_RegExp': case 'throws_RegExp':
bench.start(); bench.start();
for (i = 0; i < n; ++i) { for (let i = 0; i < n; ++i) {
throws(throwError, regExp, message); throws(throwError, regExp, message);
} }
bench.end(n); bench.end(n);