Skip to content

Commit

Permalink
benchmark: improve assert benchmarks
Browse files Browse the repository at this point in the history
This reduces the runtime and makes sure the strict and loose options
can be tested individually.

Besides that a couple of redundant cases were removed.

PR-URL: #22211
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and rvagg committed Aug 15, 2018
1 parent a414b07 commit 1c57701
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 189 deletions.
14 changes: 8 additions & 6 deletions benchmark/assert/deepequal-buffer.js
Expand Up @@ -3,17 +3,16 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [1e5],
len: [1e2, 1e4],
n: [2e4],
len: [1e2, 1e3],
strict: [0, 1],
method: [
'deepEqual',
'deepStrictEqual',
'notDeepEqual',
'notDeepStrictEqual'
'notDeepEqual'
]
});

function main({ len, n, method }) {
function main({ len, n, method, strict }) {
if (!method)
method = 'deepEqual';
const data = Buffer.allocUnsafe(len + 1);
Expand All @@ -24,6 +23,9 @@ function main({ len, n, method }) {
data.copy(expected);
data.copy(expectedWrong);

if (strict) {
method = method.replace('eep', 'eepStrict');
}
const fn = assert[method];
const value2 = method.includes('not') ? expectedWrong : expected;

Expand Down
66 changes: 9 additions & 57 deletions benchmark/assert/deepequal-map.js
Expand Up @@ -7,21 +7,14 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
const bench = common.createBenchmark(main, {
n: [5e2],
len: [5e2],
strict: [0, 1],
method: [
'deepEqual_primitiveOnly',
'deepStrictEqual_primitiveOnly',
'deepEqual_objectOnly',
'deepStrictEqual_objectOnly',
'deepEqual_mixed',
'deepStrictEqual_mixed',
'deepEqual_looseMatches',
'notDeepEqual_primitiveOnly',
'notDeepStrictEqual_primitiveOnly',
'notDeepEqual_objectOnly',
'notDeepStrictEqual_objectOnly',
'notDeepEqual_mixed',
'notDeepStrictEqual_mixed',
'notDeepEqual_looseMatches',
'notDeepEqual_mixed'
]
});

Expand All @@ -37,7 +30,7 @@ function benchmark(method, n, values, values2) {
bench.end(n);
}

function main({ n, len, method }) {
function main({ n, len, method, strict }) {
const array = Array(len).fill(1);
var values, values2;

Expand All @@ -46,74 +39,33 @@ function main({ n, len, method }) {
// Empty string falls through to next line as default, mostly for tests.
case 'deepEqual_primitiveOnly':
values = array.map((_, i) => [`str_${i}`, 123]);
benchmark(deepEqual, n, values);
break;
case 'deepStrictEqual_primitiveOnly':
values = array.map((_, i) => [`str_${i}`, 123]);
benchmark(deepStrictEqual, n, values);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break;
case 'deepEqual_objectOnly':
values = array.map((_, i) => [[`str_${i}`, 1], 123]);
benchmark(deepEqual, n, values);
break;
case 'deepStrictEqual_objectOnly':
values = array.map((_, i) => [[`str_${i}`, 1], 123]);
benchmark(deepStrictEqual, n, values);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break;
case 'deepEqual_mixed':
values = array.map((_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]);
benchmark(deepEqual, n, values);
break;
case 'deepStrictEqual_mixed':
values = array.map((_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]);
benchmark(deepStrictEqual, n, values);
break;
case 'deepEqual_looseMatches':
values = array.map((_, i) => [i, i]);
values2 = values.slice().map((v) => [String(v[0]), String(v[1])]);
benchmark(deepEqual, n, values, values2);
benchmark(strict ? deepStrictEqual : deepEqual, n, values);
break;
case 'notDeepEqual_primitiveOnly':
values = array.map((_, i) => [`str_${i}`, 123]);
values2 = values.slice(0);
values2[Math.floor(len / 2)] = ['w00t', 123];
benchmark(notDeepEqual, n, values, values2);
break;
case 'notDeepStrictEqual_primitiveOnly':
values = array.map((_, i) => [`str_${i}`, 123]);
values2 = values.slice(0);
values2[Math.floor(len / 2)] = ['w00t', 123];
benchmark(notDeepStrictEqual, n, values, values2);
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break;
case 'notDeepEqual_objectOnly':
values = array.map((_, i) => [[`str_${i}`, 1], 123]);
values2 = values.slice(0);
values2[Math.floor(len / 2)] = [['w00t'], 123];
benchmark(notDeepEqual, n, values, values2);
break;
case 'notDeepStrictEqual_objectOnly':
values = array.map((_, i) => [[`str_${i}`, 1], 123]);
values2 = values.slice(0);
values2[Math.floor(len / 2)] = [['w00t'], 123];
benchmark(notDeepStrictEqual, n, values, values2);
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break;
case 'notDeepEqual_mixed':
values = array.map((_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]);
values2 = values.slice(0);
values2[0] = ['w00t', 123];
benchmark(notDeepEqual, n, values, values2);
break;
case 'notDeepStrictEqual_mixed':
values = array.map((_, i) => [i % 2 ? [`str_${i}`, 1] : `str_${i}`, 123]);
values2 = values.slice(0);
values2[0] = ['w00t', 123];
benchmark(notDeepStrictEqual, n, values, values2);
break;
case 'notDeepEqual_looseMatches':
values = array.map((_, i) => [i, i]);
values2 = values.slice().map((v) => [String(v[0]), String(v[1])]);
values2[len - 1] = [String(len + 1), String(len + 1)];
benchmark(notDeepEqual, n, values, values2);
benchmark(strict ? notDeepStrictEqual : notDeepEqual, n, values, values2);
break;
default:
throw new Error(`Unsupported method ${method}`);
Expand Down
20 changes: 12 additions & 8 deletions benchmark/assert/deepequal-object.js
Expand Up @@ -4,13 +4,12 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [1e6],
size: [1e2, 1e3, 1e4],
n: [5e3],
size: [1e2, 1e3, 5e4],
strict: [0, 1],
method: [
'deepEqual',
'deepStrictEqual',
'notDeepEqual',
'notDeepStrictEqual'
'notDeepEqual'
]
});

Expand All @@ -20,14 +19,16 @@ function createObj(source, add = '') {
nope: {
bar: `123${add}`,
a: [1, 2, 3],
baz: n
baz: n,
c: {},
b: []
}
}));
}

function main({ size, n, method }) {
function main({ size, n, method, strict }) {
// TODO: Fix this "hack". `n` should not be manipulated.
n = n / size;
n = Math.min(Math.ceil(n / size), 20);

if (!method)
method = 'deepEqual';
Expand All @@ -37,6 +38,9 @@ function main({ size, n, method }) {
const expected = createObj(source);
const expectedWrong = createObj(source, '4');

if (strict) {
method = method.replace('eep', 'eepStrict');
}
const fn = assert[method];
const value2 = method.includes('not') ? expectedWrong : expected;

Expand Down
38 changes: 10 additions & 28 deletions benchmark/assert/deepequal-prims-and-objs-big-array-set.js
Expand Up @@ -5,29 +5,22 @@ const { deepEqual, deepStrictEqual, notDeepEqual, notDeepStrictEqual } =
require('assert');

const primValues = {
'null': null,
'undefined': undefined,
'string': 'a',
'number': 1,
'boolean': true,
'object': { 0: 'a' },
'array': [1, 2, 3],
'new-array': new Array([1, 2, 3])
'array': [1, 2, 3]
};

const bench = common.createBenchmark(main, {
primitive: Object.keys(primValues),
n: [25],
len: [1e5],
len: [2e4],
strict: [0, 1],
method: [
'deepEqual_Array',
'deepStrictEqual_Array',
'notDeepEqual_Array',
'notDeepStrictEqual_Array',
'deepEqual_Set',
'deepStrictEqual_Set',
'notDeepEqual_Set',
'notDeepStrictEqual_Set'
'notDeepEqual_Set'
]
});

Expand All @@ -39,7 +32,7 @@ function run(fn, n, actual, expected) {
bench.end(n);
}

function main({ n, len, primitive, method }) {
function main({ n, len, primitive, method, strict }) {
const prim = primValues[primitive];
const actual = [];
const expected = [];
Expand All @@ -62,28 +55,17 @@ function main({ n, len, primitive, method }) {
// Empty string falls through to next line as default, mostly for tests.
case '':
case 'deepEqual_Array':
run(deepEqual, n, actual, expected);
break;
case 'deepStrictEqual_Array':
run(deepStrictEqual, n, actual, expected);
run(strict ? deepStrictEqual : deepEqual, n, actual, expected);
break;
case 'notDeepEqual_Array':
run(notDeepEqual, n, actual, expectedWrong);
break;
case 'notDeepStrictEqual_Array':
run(notDeepStrictEqual, n, actual, expectedWrong);
run(strict ? notDeepStrictEqual : notDeepEqual, n, actual, expectedWrong);
break;
case 'deepEqual_Set':
run(deepEqual, n, actualSet, expectedSet);
break;
case 'deepStrictEqual_Set':
run(deepStrictEqual, n, actualSet, expectedSet);
run(strict ? deepStrictEqual : deepEqual, n, actualSet, expectedSet);
break;
case 'notDeepEqual_Set':
run(notDeepEqual, n, actualSet, expectedWrongSet);
break;
case 'notDeepStrictEqual_Set':
run(notDeepStrictEqual, n, actualSet, expectedWrongSet);
run(strict ? notDeepStrictEqual : notDeepEqual,
n, actualSet, expectedWrongSet);
break;
default:
throw new Error(`Unsupported method "${method}"`);
Expand Down
16 changes: 7 additions & 9 deletions benchmark/assert/deepequal-prims-and-objs-big-loop.js
Expand Up @@ -3,35 +3,33 @@ const common = require('../common.js');
const assert = require('assert');

const primValues = {
'null': null,
'undefined': undefined,
'string': 'a',
'number': 1,
'boolean': true,
'object': { 0: 'a' },
'array': [1, 2, 3],
'new-array': new Array([1, 2, 3])
'array': [1, 2, 3]
};

const bench = common.createBenchmark(main, {
primitive: Object.keys(primValues),
n: [1e6],
n: [2e4],
strict: [0, 1],
method: [
'deepEqual',
'deepStrictEqual',
'notDeepEqual',
'notDeepStrictEqual'
]
});

function main({ n, primitive, method }) {
function main({ n, primitive, method, strict }) {
if (!method)
method = 'deepEqual';
const prim = primValues[primitive];
const actual = prim;
const expected = prim;
const expectedWrong = 'b';

if (strict) {
method = method.replace('eep', 'eepStrict');
}
const fn = assert[method];
const value2 = method.includes('not') ? expectedWrong : expected;

Expand Down

0 comments on commit 1c57701

Please sign in to comment.