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