Skip to content

Commit

Permalink
benchmark: add assert.deep[Strict]Equal benchmarks
Browse files Browse the repository at this point in the history
* Move numbers into configuration
* Add buffer comparison benchmark
* Add assert.deepStrictEqual benchmarks

PR-URL: #11092
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Feb 6, 2017
1 parent 6c7fbd7 commit 5e4545e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 47 deletions.
40 changes: 40 additions & 0 deletions benchmark/assert/deepequal-buffer.js
@@ -0,0 +1,40 @@
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
n: [1e3],
len: [1e2],
method: ['strict', 'nonstrict']
});

function main(conf) {
const n = +conf.n;
const len = +conf.len;
var i;

const data = Buffer.allocUnsafe(len);
const actual = Buffer.alloc(len);
const expected = Buffer.alloc(len);
data.copy(actual);
data.copy(expected);

switch (conf.method) {
case 'strict':
bench.start();
for (i = 0; i < n; ++i) {
// eslint-disable-next-line no-restricted-properties
assert.deepEqual(actual, expected);
}
bench.end(n);
break;
case 'nonstrict':
bench.start();
for (i = 0; i < n; ++i) {
assert.deepStrictEqual(actual, expected);
}
bench.end(n);
break;
default:
throw new Error('Unsupported method');
}
}
52 changes: 33 additions & 19 deletions benchmark/assert/deepequal-prims-and-objs-big-array.js
@@ -1,6 +1,6 @@
'use strict'; 'use strict';
var common = require('../common.js'); const common = require('../common.js');
var assert = require('assert'); const assert = require('assert');


const primValues = { const primValues = {
'null': null, 'null': null,
Expand All @@ -13,29 +13,43 @@ const primValues = {
'new-array': new Array([1, 2, 3]) 'new-array': new Array([1, 2, 3])
}; };


var bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
prim: Object.keys(primValues), prim: Object.keys(primValues),
n: [25] n: [25],
len: [1e5],
method: ['strict', 'nonstrict']
}); });


function main(conf) { function main(conf) {
var prim = primValues[conf.prim]; const prim = primValues[conf.prim];
var n = +conf.n; const n = +conf.n;
var primArray; const len = +conf.len;
var primArrayCompare; const actual = [];
var x; const expected = [];
var i;


primArray = new Array(); for (var x = 0; x < len; x++) {
primArrayCompare = new Array(); actual.push(prim);
for (x = 0; x < (1e5); x++) { expected.push(prim);
primArray.push(prim);
primArrayCompare.push(prim);
} }


bench.start(); switch (conf.method) {
for (x = 0; x < n; x++) { case 'strict':
// eslint-disable-next-line no-restricted-properties bench.start();
assert.deepEqual(primArray, primArrayCompare); for (i = 0; i < n; ++i) {
// eslint-disable-next-line no-restricted-properties
assert.deepEqual(actual, expected);
}
bench.end(n);
break;
case 'nonstrict':
bench.start();
for (i = 0; i < n; ++i) {
assert.deepStrictEqual(actual, expected);
}
bench.end(n);
break;
default:
throw new Error('Unsupported method');
} }
bench.end(n);
} }
43 changes: 29 additions & 14 deletions benchmark/assert/deepequal-prims-and-objs-big-loop.js
@@ -1,6 +1,6 @@
'use strict'; 'use strict';
var common = require('../common.js'); const common = require('../common.js');
var assert = require('assert'); const assert = require('assert');


const primValues = { const primValues = {
'null': null, 'null': null,
Expand All @@ -13,22 +13,37 @@ const primValues = {
'new-array': new Array([1, 2, 3]) 'new-array': new Array([1, 2, 3])
}; };


var bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
prim: Object.keys(primValues), prim: Object.keys(primValues),
n: [1e5] n: [1e6],
method: ['strict', 'nonstrict']
}); });


function main(conf) { function main(conf) {
var prim = primValues[conf.prim]; const prim = primValues[conf.prim];
var n = +conf.n; const n = +conf.n;
var x; const actual = prim;
const expected = prim;
var i;


bench.start(); // Creates new array to avoid loop invariant code motion

switch (conf.method) {
for (x = 0; x < n; x++) { case 'strict':
// eslint-disable-next-line no-restricted-properties bench.start();
assert.deepEqual(new Array([prim]), new Array([prim])); for (i = 0; i < n; ++i) {
// eslint-disable-next-line no-restricted-properties
assert.deepEqual([actual], [expected]);
}
bench.end(n);
break;
case 'nonstrict':
bench.start();
for (i = 0; i < n; ++i) {
assert.deepStrictEqual([actual], [expected]);
}
bench.end(n);
break;
default:
throw new Error('Unsupported method');
} }

bench.end(n);
} }
46 changes: 32 additions & 14 deletions benchmark/assert/deepequal-typedarrays.js
@@ -1,23 +1,41 @@
'use strict'; 'use strict';
var common = require('../common.js'); const common = require('../common.js');
var assert = require('assert'); const assert = require('assert');
var bench = common.createBenchmark(main, { const bench = common.createBenchmark(main, {
type: ('Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array ' + type: ('Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array ' +
'Float32Array Float64Array Uint8ClampedArray').split(' '), 'Float32Array Float64Array Uint8ClampedArray').split(' '),
n: [1] n: [1],
method: ['strict', 'nonstrict'],
len: [1e6]
}); });


function main(conf) { function main(conf) {
var type = conf.type; const type = conf.type;
var clazz = global[type]; const clazz = global[type];
var n = +conf.n; const n = +conf.n;
const len = +conf.len;


bench.start(); const actual = new clazz(len);
var actual = new clazz(n * 1e6); const expected = new clazz(len);
var expected = new clazz(n * 1e6); var i;


// eslint-disable-next-line no-restricted-properties switch (conf.method) {
assert.deepEqual(actual, expected); case 'strict':

bench.start();
bench.end(n); for (i = 0; i < n; ++i) {
// eslint-disable-next-line no-restricted-properties
assert.deepEqual(actual, expected);
}
bench.end(n);
break;
case 'nonstrict':
bench.start();
for (i = 0; i < n; ++i) {
assert.deepStrictEqual(actual, expected);
}
bench.end(n);
break;
default:
throw new Error('Unsupported method');
}
} }

0 comments on commit 5e4545e

Please sign in to comment.