<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/jsondiffpatch@0.4.1/dist/jsondiffpatch.umd.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...'
}
};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = structuredClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash cloneDeep | |
JSON.parse(JSON.stringify()) | |
structuredClone |
Test name | Executions per second |
---|---|
lodash cloneDeep | 989156.6 Ops/sec |
JSON.parse(JSON.stringify()) | 810585.6 Ops/sec |
structuredClone | 369258.9 Ops/sec |
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test compares three approaches to create a deep copy of an object:
JSON.parse()
function to parse a JSON string representation of the original object and then creates a new object with the same properties.Options Comparison:
Library Usage:
The structuredClone()
function is part of the Web API and does not require an external library. The _.cloneDeep()
function from Lodash requires an additional library to be included.
Special JS Feature/Syntax:
The test case uses the new JavaScript feature structuredClone()
, which was introduced in ECMAScript 2020. It's a relatively new addition to the language and might not work in older browsers or environments.
Benchmark Results:
The latest benchmark results show that:
Overall, the test case demonstrates that while all three approaches can create deep copies of objects, structuredClone() is currently the most efficient and reliable option.