Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
(version: 0)
Comparing performance of:
lodash cloneDeep vs JSON.parse(JSON.stringify()) vs structuredClone
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<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>
Script Preparation code:
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;
Tests:
lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
JSON.parse(JSON.stringify())
myCopy = JSON.parse(JSON.stringify(MyObject));
structuredClone
myCopy = structuredClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash cloneDeep
JSON.parse(JSON.stringify())
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash cloneDeep
1663171.2 Ops/sec
JSON.parse(JSON.stringify())
1282401.9 Ops/sec
structuredClone
526226.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **_.cloneDeep() from Lodash**: Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including cloning objects. 2. **JSON.parse(JSON.stringify())**: This approach uses the `JSON.parse()` function to parse a JSON string representation of the original object and then creates a new object with the same properties. 3. **structuredClone()**: This is a newer JavaScript feature introduced in ECMAScript 2020, which provides a way to create a deep clone of an object without mutating the original object. **Options Comparison:** * **Pros of _.cloneDeep():** * Well-established and widely supported library * Provides a simple and intuitive API for cloning objects * Can handle complex objects with nested structures * **Cons of _.cloneDeep():** * Requires an additional library (Lodash) * May not be the most efficient option, especially for very large objects * **Pros of JSON.parse(JSON.stringify()):** * Built-in JavaScript function, so no external library is required * Can be faster than _.cloneDeep() for simple objects * Uses a specific syntax that can be useful in certain scenarios * **Cons of JSON.parse(JSON.stringify()):** * Can fail if the original object contains circular references or non-JSON data types * May not handle complex objects with nested structures as well as _.cloneDeep() * Uses more CPU cycles than structuredClone() for large objects * **Pros of structuredClone():** * Newer JavaScript feature, so it's only available in recent browsers and Node.js versions * Handles circular references and non-JSON data types correctly * Faster and more efficient than _.cloneDeep() and JSON.parse(JSON.stringify()) for large objects * **Cons of structuredClone():** * Not widely supported yet, so it may not work in older browsers or environments * Requires a recent JavaScript engine that supports the feature **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: * **_.cloneDeep()` is the fastest option, with an average execution rate of approximately 989 million executions per second. * **JSON.parse(JSON.stringify())` is slower than _.cloneDeep(), but still relatively fast, with an average execution rate of around 810 million executions per second. * **structuredClone()` is the slowest option, with an average execution rate of approximately 369 million executions per second. 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.
Related benchmarks:
Lodash cloneDeep vs JSON.parse(JSON.stringify())
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash vs structured Clone vs json parse
Lodash cloneDeep vs structuredClone vs Json.stringify
Comments
Confirm delete:
Do you really want to delete benchmark?