From 8f34181b9517b5025ff59f6449ab822b7255ed18 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 1 Feb 2017 07:28:59 +0800 Subject: [PATCH] benchmark: add assert.deep[Strict]Equal benchmarks * Move numbers into configuration * Add buffer comparison benchmark * Add assert.deepStrictEqual benchmarks PR-URL: https://github.com/nodejs/node/pull/11092 Reviewed-By: James M Snell --- benchmark/assert/deepequal-buffer.js | 40 ++++++++++++++ .../deepequal-prims-and-objs-big-array.js | 52 ++++++++++++------- .../deepequal-prims-and-objs-big-loop.js | 43 ++++++++++----- benchmark/assert/deepequal-typedarrays.js | 46 +++++++++++----- 4 files changed, 134 insertions(+), 47 deletions(-) create mode 100644 benchmark/assert/deepequal-buffer.js diff --git a/benchmark/assert/deepequal-buffer.js b/benchmark/assert/deepequal-buffer.js new file mode 100644 index 00000000000000..2a7d9e3bed7c38 --- /dev/null +++ b/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'); + } +} diff --git a/benchmark/assert/deepequal-prims-and-objs-big-array.js b/benchmark/assert/deepequal-prims-and-objs-big-array.js index 1b4802c8ff4ac2..69eda8af087dfa 100644 --- a/benchmark/assert/deepequal-prims-and-objs-big-array.js +++ b/benchmark/assert/deepequal-prims-and-objs-big-array.js @@ -1,6 +1,6 @@ 'use strict'; -var common = require('../common.js'); -var assert = require('assert'); +const common = require('../common.js'); +const assert = require('assert'); const primValues = { 'null': null, @@ -13,29 +13,43 @@ const primValues = { 'new-array': new Array([1, 2, 3]) }; -var bench = common.createBenchmark(main, { +const bench = common.createBenchmark(main, { prim: Object.keys(primValues), - n: [25] + n: [25], + len: [1e5], + method: ['strict', 'nonstrict'] }); function main(conf) { - var prim = primValues[conf.prim]; - var n = +conf.n; - var primArray; - var primArrayCompare; - var x; + const prim = primValues[conf.prim]; + const n = +conf.n; + const len = +conf.len; + const actual = []; + const expected = []; + var i; - primArray = new Array(); - primArrayCompare = new Array(); - for (x = 0; x < (1e5); x++) { - primArray.push(prim); - primArrayCompare.push(prim); + for (var x = 0; x < len; x++) { + actual.push(prim); + expected.push(prim); } - bench.start(); - for (x = 0; x < n; x++) { - // eslint-disable-next-line no-restricted-properties - assert.deepEqual(primArray, primArrayCompare); + 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'); } - bench.end(n); } diff --git a/benchmark/assert/deepequal-prims-and-objs-big-loop.js b/benchmark/assert/deepequal-prims-and-objs-big-loop.js index dea084bc984126..781c5ad754e723 100644 --- a/benchmark/assert/deepequal-prims-and-objs-big-loop.js +++ b/benchmark/assert/deepequal-prims-and-objs-big-loop.js @@ -1,6 +1,6 @@ 'use strict'; -var common = require('../common.js'); -var assert = require('assert'); +const common = require('../common.js'); +const assert = require('assert'); const primValues = { 'null': null, @@ -13,22 +13,37 @@ const primValues = { 'new-array': new Array([1, 2, 3]) }; -var bench = common.createBenchmark(main, { +const bench = common.createBenchmark(main, { prim: Object.keys(primValues), - n: [1e5] + n: [1e6], + method: ['strict', 'nonstrict'] }); function main(conf) { - var prim = primValues[conf.prim]; - var n = +conf.n; - var x; + const prim = primValues[conf.prim]; + const n = +conf.n; + const actual = prim; + const expected = prim; + var i; - bench.start(); - - for (x = 0; x < n; x++) { - // eslint-disable-next-line no-restricted-properties - assert.deepEqual(new Array([prim]), new Array([prim])); + // Creates new array to avoid loop invariant code motion + 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'); } - - bench.end(n); } diff --git a/benchmark/assert/deepequal-typedarrays.js b/benchmark/assert/deepequal-typedarrays.js index 1954c57ee59eeb..037cfb2cf1ec3c 100644 --- a/benchmark/assert/deepequal-typedarrays.js +++ b/benchmark/assert/deepequal-typedarrays.js @@ -1,23 +1,41 @@ 'use strict'; -var common = require('../common.js'); -var assert = require('assert'); -var bench = common.createBenchmark(main, { +const common = require('../common.js'); +const assert = require('assert'); +const bench = common.createBenchmark(main, { type: ('Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array ' + 'Float32Array Float64Array Uint8ClampedArray').split(' '), - n: [1] + n: [1], + method: ['strict', 'nonstrict'], + len: [1e6] }); function main(conf) { - var type = conf.type; - var clazz = global[type]; - var n = +conf.n; + const type = conf.type; + const clazz = global[type]; + const n = +conf.n; + const len = +conf.len; - bench.start(); - var actual = new clazz(n * 1e6); - var expected = new clazz(n * 1e6); + const actual = new clazz(len); + const expected = new clazz(len); + var i; - // eslint-disable-next-line no-restricted-properties - assert.deepEqual(actual, expected); - - bench.end(n); + 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'); + } }