<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],
l: [4,5,6]
},
},
e: [1,2,3,4,5,6],
f: 1,
g: {
h: 1,
}
}
const a = _.clone(o)
const a = _.cloneDeep(o)
const a = JSON.parse(JSON.stringify(o))
const a = structuredClone(o)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash clone | |
Lodash cloneDeep | |
JSON.parse(JSON.stringify(o)) | |
structuredClone |
Test name | Executions per second |
---|---|
Lodash clone | 3340079.5 Ops/sec |
Lodash cloneDeep | 501803.0 Ops/sec |
JSON.parse(JSON.stringify(o)) | 833871.2 Ops/sec |
structuredClone | 482286.3 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and their pros/cons.
Benchmark Overview
The benchmark compares four different methods for cloning or manipulating objects in JavaScript:
clone
cloneDeep
JSON.parse(JSON.stringify(o))
(also known as "deep copying")structuredClone
These methods are compared to measure their performance, accuracy, and any potential differences.
Method 1: Lodash clone
Lodash's clone
function creates a shallow copy of an object, which means it only copies the top-level properties and not nested objects or arrays. This can lead to issues if you're trying to clone deeply nested structures.
Pros:
Cons:
Method 2: Lodash cloneDeep
Lodash's cloneDeep
function creates a deep copy of an object, which means it recursively copies all nested objects and arrays. This is more suitable for cloning complex data structures.
Pros:
Cons:
clone
Method 3: Using JSON.parse(JSON.stringify(o))
This method uses the fact that in JSON, objects are serialized to a flat representation. When deserializing this string back into an object, it creates a deep copy of the original object.
Pros:
Cons:
Method 4: structuredClone
This method is a relatively new addition to JavaScript, introduced in ES2022. It creates a deep copy of an object, preserving all its properties, including functions and other non-serializable values.
Pros:
Cons: None mentioned yet (since it's a relatively new feature).
Library Explanation
The benchmark uses Lodash as the library for the clone
and cloneDeep
functions. Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object cloning, and more.
In this specific benchmark, Lodash's clone
function creates a shallow copy of an object, while cloneDeep
creates a deep copy. The structuredClone
method is used as-is since it's introduced in ES2022.
Other Considerations
The test cases use the same input object o
, which contains various nested structures (objects, arrays, functions). This allows us to compare the performance and accuracy of each method on similar inputs.
It's worth noting that some methods may have specific edge cases or limitations. For example, Lodash's clone
function may not work correctly if the original object contains circular references.
Alternatives
If you're looking for alternative JavaScript libraries or built-in functions for object cloning or manipulation, here are a few options:
clone
and cloneDeep
.Object.assign()
or Object.create()
can be used to create a shallow copy of an object.JSON.parse(JSON.stringify(o))
(described above) can be used as a deep copy method.However, keep in mind that these alternatives may have different performance characteristics, accuracy, and usage scenarios compared to the methods tested in this benchmark.