From: Jérôme Benoit Date: Mon, 26 Dec 2022 12:09:37 +0000 (+0100) Subject: Cleanup object generation code X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5b907dbf7ed955584eb9f452f9dc4c8f59577816;p=benchmarks-js.git Cleanup object generation code Signed-off-by: Jérôme Benoit --- diff --git a/benchmark-utils.js b/benchmark-utils.js index 5694a97..9c82d84 100644 --- a/benchmark-utils.js +++ b/benchmark-utils.js @@ -61,6 +61,26 @@ function generateRandomNumberArray ( return array } +/** + * + * @param sizeMax + * @param numberMax + * @param numberGenerator + * @returns + */ +function generateRandomObject ( + sizeMax = 500, + numberMax = Number.MAX_VALUE, + numberGenerator = generateRandomFloat +) { + const size = generateRandomInteger(sizeMax) + const object = {} + for (let i = 0; i < size; i++) { + object[i.toString()] = numberGenerator(numberMax) + } + return { object, size } +} + /** * @param ms * @returns @@ -73,6 +93,7 @@ module.exports = { generateRandomInteger, generateRandomFloat, generateRandomNumberArray, + generateRandomObject, sleep, secureRandom } diff --git a/deep-clone-object.js b/deep-clone-object.js index de345fc..e1d8b1c 100644 --- a/deep-clone-object.js +++ b/deep-clone-object.js @@ -1,27 +1,23 @@ /* eslint-disable no-unused-vars */ const Benchmark = require('benny') -const { generateRandomInteger } = require('./benchmark-utils') +const { generateRandomObject } = require('./benchmark-utils') const _ = require('lodash') const clone = require('just-clone') -const size = generateRandomInteger(500) -const testObject = {} -for (let i = 0; i < size; i++) { - testObject[i.toString()] = i -} +const { object, size } = generateRandomObject() Benchmark.suite( `Deep clone object with ${size} keys`, - Benchmark.add('JSON stringify/parse', (obj = testObject) => { + Benchmark.add('JSON stringify/parse', (obj = object) => { const objClone = JSON.parse(JSON.stringify(obj)) }), - Benchmark.add('structuredClone', (obj = testObject) => { + Benchmark.add('structuredClone', (obj = object) => { const objClone = structuredClone(obj) }), - Benchmark.add('lodash cloneDeep', (obj = testObject) => { + Benchmark.add('lodash cloneDeep', (obj = object) => { const objClone = _.cloneDeep(obj) }), - Benchmark.add('just-clone', (obj = testObject) => { + Benchmark.add('just-clone', (obj = object) => { const objClone = clone(obj) }), Benchmark.cycle(), diff --git a/is-empty-object.js b/is-empty-object.js index 43059bb..b1f4d76 100644 --- a/is-empty-object.js +++ b/is-empty-object.js @@ -1,18 +1,14 @@ const Benchmark = require('benny') -const { generateRandomInteger } = require('./benchmark-utils') +const { generateRandomObject } = require('./benchmark-utils') -const size = generateRandomInteger(500) -const testObject = {} -for (let i = 0; i < size; i++) { - testObject[i.toString()] = i -} +const { object, size } = generateRandomObject() Benchmark.suite( `Is empty object with ${size} keys`, - Benchmark.add('Reflect keys', (obj = testObject) => { + Benchmark.add('Reflect keys', (obj = object) => { return obj?.constructor === Object && Reflect.ownKeys(obj).length === 0 }), - Benchmark.add('Keys iteration', (obj = testObject) => { + Benchmark.add('Keys iteration', (obj = object) => { if (obj?.constructor !== Object) return false // Iterates over the keys of an object, if // any exist, return false. @@ -20,7 +16,7 @@ Benchmark.suite( for (const _ in obj) return false return true }), - Benchmark.add('Object keys', (obj = testObject) => { + Benchmark.add('Object keys', (obj = object) => { return obj?.constructor === Object && Object.keys(obj).length === 0 }), Benchmark.cycle(), diff --git a/shallow-clone-object.js b/shallow-clone-object.js index 131b5e9..18c396d 100644 --- a/shallow-clone-object.js +++ b/shallow-clone-object.js @@ -1,23 +1,19 @@ /* eslint-disable no-unused-vars */ const Benchmark = require('benny') -const { generateRandomInteger } = require('./benchmark-utils') +const { generateRandomObject } = require('./benchmark-utils') const _ = require('lodash') -const size = generateRandomInteger(500) -const testObject = {} -for (let i = 0; i < size; i++) { - testObject[i.toString()] = i -} +const { object, size } = generateRandomObject() Benchmark.suite( `Shallow clone object with ${size} keys`, - Benchmark.add('Spread', (obj = testObject) => { + Benchmark.add('Spread', (obj = object) => { const objClone = { ...obj } }), - Benchmark.add('Object assign', (obj = testObject) => { + Benchmark.add('Object assign', (obj = object) => { const objClone = Object.assign({}, obj) }), - Benchmark.add('lodash clone', (obj = testObject) => { + Benchmark.add('lodash clone', (obj = object) => { const objClone = _.clone(obj) }), Benchmark.cycle(),