Add object cloning benchmark
[benchmarks-js.git] / 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(
13 `Clone object with ${size} keys`,
14 Benchmark.add('structuredClone', (obj = testObject) => {
15 const objClone = structuredClone(obj)
16 }),
17 Benchmark.add('Spread', (obj = testObject) => {
18 const objClone = { ...obj }
19 }),
20 Benchmark.add('Object assign', (obj = testObject) => {
21 const objClone = Object.assign({}, obj)
22 }),
23 Benchmark.add('JSON stringify/parse', (obj = testObject) => {
24 const objClone = JSON.parse(JSON.stringify(obj))
25 }),
26 Benchmark.add('lodash clone', (obj = testObject) => {
27 const objClone = _.clone(obj)
28 }),
29 Benchmark.add('lodash deep clone', (obj = testObject) => {
30 const objClone = _.cloneDeep(obj)
31 }),
32 Benchmark.cycle(),
33 Benchmark.complete(),
34 Benchmark.save({
35 file: 'clone-object',
36 format: 'json',
37 details: true
38 }),
39 Benchmark.save({
40 file: 'clone-object',
41 format: 'chart.html',
42 details: true
43 }),
44 Benchmark.save({
45 file: 'clone-object',
46 format: 'table.html',
47 details: true
48 })
49)