<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var o = {
a: {
b: 1,
c: 2,
d: 3,
j: {
k: [1,2,3].fill('x', 2, 2000),
l: [4,5,6].fill(0, 2, 4000)
},
},
e: [1,2,3,4,5,6],
f: 1,
g: {
h: 1,
}
}
const a = _.clone(o)
const a = _.cloneDeep(o)
const a = _.merge({}, o)
const a = { o }
const a = Object.assign({}, o)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash clone | |
Lodash cloneDeep | |
Lodash merge | |
ES6 spread | |
ES6 Object.assign |
Test name | Executions per second |
---|---|
Lodash clone | 1012929.8 Ops/sec |
Lodash cloneDeep | 139049.4 Ops/sec |
Lodash merge | 89523.1 Ops/sec |
ES6 spread | 10801710.0 Ops/sec |
ES6 Object.assign | 3226088.0 Ops/sec |
Let's break down what is being tested in the provided JSON.
Benchmark Definition
The benchmark tests the performance of different approaches to create a shallow copy of an object. There are five test cases:
_.clone()
function from the Lodash library to create a copy of the original object._.cloneDeep()
function from the Lodash library to create a deep copy of the original object._.merge()
function from the Lodash library to merge the original object with an empty object, effectively creating a shallow copy.{ ...o }
to create a new object with all properties from the original object o
.Object.assign()
function to create a new object with all properties from the original object o
.Options Compared
The benchmark compares the performance of these five approaches:
clone
function performs a shallow copy, while cloneDeep
performs a deep copy.merge
uses the _.merge()
function from Lodash, which recursively merges objects, while spread
uses the syntax { ...o }
, which creates a new object by spreading the properties of o
.Pros and Cons
Here are some pros and cons of each approach:
clone
.Library Usage
The benchmark uses the Lodash library for the clone
, cloneDeep
, and merge
functions. The Object.assign()
function is a built-in JavaScript function that does not require any external libraries.
Special JS Features/Syntax
There are no special JavaScript features or syntax used in this benchmark, aside from the ES6 spread syntax { ...o }
.