Differentiate shallow and deep clone implementation benchmarks
[benchmarks-js.git] / shallow-clone-object.js
1 /* eslint-disable no-unused-vars */
2 const Benchmark = require('benny')
3 const { generateRandomInteger } = require('./benchmark-utils')
4 const _ = require('lodash')
5
6 const size = generateRandomInteger(500)
7 const testObject = {}
8 for (let i = 0; i < size; i++) {
9 testObject[i.toString()] = i
10 }
11
12 Benchmark.suite(
13 `Shallow clone object with ${size} keys`,
14 Benchmark.add('Spread', (obj = testObject) => {
15 const objClone = { ...obj }
16 }),
17 Benchmark.add('Object assign', (obj = testObject) => {
18 const objClone = Object.assign({}, obj)
19 }),
20 Benchmark.add('lodash clone', (obj = testObject) => {
21 const objClone = _.clone(obj)
22 }),
23 Benchmark.cycle(),
24 Benchmark.complete(),
25 Benchmark.save({
26 file: 'shallow-clone-object',
27 format: 'json',
28 details: true
29 }),
30 Benchmark.save({
31 file: 'shallow-clone-object',
32 format: 'chart.html',
33 details: true
34 }),
35 Benchmark.save({
36 file: 'shallow-clone-object',
37 format: 'table.html',
38 details: true
39 })
40 )