<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var MyObject = {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
deeper: {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
deeper: {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
wayWayDown: {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
date: new Date(),
set: new Set([1,2,3,4,5]),
map: new Map([['a','b'],['c','d']])
}
}
}
};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = structuredClone(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Native structuredClone | |
JSON Parse |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 128500.2 Ops/sec |
Native structuredClone | 108825.9 Ops/sec |
JSON Parse | 135767.5 Ops/sec |
Measuring the performance of different cloning methods in JavaScript can be a valuable exercise, especially when comparing the efficiency of libraries like Lodash with native implementations.
Benchmark Definition
The benchmark defines three test cases:
_.cloneDeep()
function from the Lodash library to create a deep copy of the input object.structuredClone()
function, which is a native JavaScript API introduced in ECMAScript 2020.JSON.parse(JSON.stringify())
trick to create a deep copy of the input object.Options Compared
The three methods are compared based on their execution time, measured in executions per second (EPS). The benchmarked values indicate that:
Pros and Cons
Here's a summary of the pros and cons for each method:
Library
The structuredClone()
function is a part of the JavaScript standard library, introduced in ECMAScript 2020. Its purpose is to create a deep copy of an object, similar to the other two methods.
Special JS Features/Syntax
No special JavaScript features or syntax are used in this benchmark, apart from the use of structuredClone()
, which is a relatively new feature introduced by the W3C.
Other Alternatives
If you need to clone objects in JavaScript and want alternatives to these methods:
In summary, when working with JavaScript object cloning, you should consider the trade-offs between efficiency, simplicity, and dependency management. Native implementations like structuredClone()
might be the best choice in modern environments, while Lodash methods provide a familiar interface for those already invested in the library.