}
Benchmark.suite(
- `Clone object with ${size} keys`,
- Benchmark.add('structuredClone', (obj = testObject) => {
- const objClone = structuredClone(obj)
- }),
- Benchmark.add('Spread', (obj = testObject) => {
- const objClone = { ...obj }
- }),
- Benchmark.add('Object assign', (obj = testObject) => {
- const objClone = Object.assign({}, obj)
- }),
+ `Deep clone object with ${size} keys`,
Benchmark.add('JSON stringify/parse', (obj = testObject) => {
const objClone = JSON.parse(JSON.stringify(obj))
}),
- Benchmark.add('lodash clone', (obj = testObject) => {
- const objClone = _.clone(obj)
+ Benchmark.add('structuredClone', (obj = testObject) => {
+ const objClone = structuredClone(obj)
}),
- Benchmark.add('lodash deep clone', (obj = testObject) => {
+ Benchmark.add('lodash cloneDeep', (obj = testObject) => {
const objClone = _.cloneDeep(obj)
}),
Benchmark.add('just-clone', (obj = testObject) => {
Benchmark.cycle(),
Benchmark.complete(),
Benchmark.save({
- file: 'clone-object',
+ file: 'deep-clone-object',
format: 'json',
details: true
}),
Benchmark.save({
- file: 'clone-object',
+ file: 'deep-clone-object',
format: 'chart.html',
details: true
}),
Benchmark.save({
- file: 'clone-object',
+ file: 'deep-clone-object',
format: 'table.html',
details: true
})
"prepare": "node prepare.js",
"benchmark:busy-wait": "node busy-wait.js",
"benchmark:empty-array": "node empty-array.js",
- "benchmark:clone-object": "node clone-object.js",
+ "benchmark:deep-clone-object": "node deep-clone-object.js",
+ "benchmark:shallow-clone-object": "node shallow-clone-object.js",
"benchmark:is-empty-object": "node is-empty-object.js",
"benchmark:is-undefined": "node is-undefined.js",
"benchmark:quick-select": "node quick-select.js",
Benchmark.suite(
'Promise handling',
Benchmark.add('await promise', async () => {
- await asyncFunction()
+ try {
+ return await asyncFunction()
+ } catch (e) {
+ console.error(e)
+ }
}),
- Benchmark.add('promise', () => {
+ Benchmark.add('promise with then().catch()', () => {
+ asyncFunction()
+ .then(r => {
+ return r
+ })
+ .catch(e => {
+ console.error(e)
+ })
+ }),
+ Benchmark.add('mishandled promise', () => {
asyncFunction()
}),
Benchmark.cycle(),
--- /dev/null
+/* eslint-disable no-unused-vars */
+const Benchmark = require('benny')
+const { generateRandomInteger } = require('./benchmark-utils')
+const _ = require('lodash')
+
+const size = generateRandomInteger(500)
+const testObject = {}
+for (let i = 0; i < size; i++) {
+ testObject[i.toString()] = i
+}
+
+Benchmark.suite(
+ `Shallow clone object with ${size} keys`,
+ Benchmark.add('Spread', (obj = testObject) => {
+ const objClone = { ...obj }
+ }),
+ Benchmark.add('Object assign', (obj = testObject) => {
+ const objClone = Object.assign({}, obj)
+ }),
+ Benchmark.add('lodash clone', (obj = testObject) => {
+ const objClone = _.clone(obj)
+ }),
+ Benchmark.cycle(),
+ Benchmark.complete(),
+ Benchmark.save({
+ file: 'shallow-clone-object',
+ format: 'json',
+ details: true
+ }),
+ Benchmark.save({
+ file: 'shallow-clone-object',
+ format: 'chart.html',
+ details: true
+ }),
+ Benchmark.save({
+ file: 'shallow-clone-object',
+ format: 'table.html',
+ details: true
+ })
+)