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
generateRandomInteger,
generateRandomFloat,
generateRandomNumberArray,
+ generateRandomObject,
sleep,
secureRandom
}
/* 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(),
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.
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(),
/* 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(),