Add just-clone to 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')
a9aa1456 5const clone = require('just-clone')
6dce9ba7
JB
6
7const size = generateRandomInteger(500)
8const testObject = {}
9for (let i = 0; i < size; i++) {
10 testObject[i.toString()] = i
11}
12
13Benchmark.suite(
14 `Clone object with ${size} keys`,
15 Benchmark.add('structuredClone', (obj = testObject) => {
16 const objClone = structuredClone(obj)
17 }),
18 Benchmark.add('Spread', (obj = testObject) => {
19 const objClone = { ...obj }
20 }),
21 Benchmark.add('Object assign', (obj = testObject) => {
22 const objClone = Object.assign({}, obj)
23 }),
24 Benchmark.add('JSON stringify/parse', (obj = testObject) => {
25 const objClone = JSON.parse(JSON.stringify(obj))
26 }),
27 Benchmark.add('lodash clone', (obj = testObject) => {
28 const objClone = _.clone(obj)
29 }),
30 Benchmark.add('lodash deep clone', (obj = testObject) => {
31 const objClone = _.cloneDeep(obj)
32 }),
a9aa1456
JB
33 Benchmark.add('just-clone', (obj = testObject) => {
34 const objClone = clone(obj)
35 }),
6dce9ba7
JB
36 Benchmark.cycle(),
37 Benchmark.complete(),
38 Benchmark.save({
39 file: 'clone-object',
40 format: 'json',
41 details: true
42 }),
43 Benchmark.save({
44 file: 'clone-object',
45 format: 'chart.html',
46 details: true
47 }),
48 Benchmark.save({
49 file: 'clone-object',
50 format: 'table.html',
51 details: true
52 })
53)