Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSON.parse + JSON.stringify
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs JSON.parse
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.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);
Native structuredClone
myCopy = structuredClone(MyObject);
JSON.parse
myCopy = JSON.parse(JSON.stringify(MyObject))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Native structuredClone
JSON.parse
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
685521.6 Ops/sec
Native structuredClone
262733.0 Ops/sec
JSON.parse
460336.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three different methods for creating a deep copy of an object in JavaScript: 1. `Lodash cloneDeep` 2. `Native structuredClone` (implemented in modern browsers) 3. `JSON.parse + JSON.stringify` **Method Comparisons** ### Lodash cloneDeep * Purpose: A popular utility function from the Lodash library that creates a deep copy of an object. * Pros: + Widely used and well-maintained library. + Provides additional features beyond basic copying (e.g., cloning arrays). * Cons: + Adds external dependency (Lodash) to the benchmarked codebase. + May not be optimized for performance, as it uses a recursive approach. ### Native structuredClone * Purpose: A modern browser API that creates a deep clone of an object. * Pros: + Optimized for performance by leveraging WebAssembly (WASM) under the hood. + No external dependency required, as it's built into the browser engine. * Cons: + Limited support for older browsers and environments. + May not work as expected with certain types of objects or data structures. ### JSON.parse + JSON.stringify * Purpose: A simple, yet inefficient method that uses `JSON.parse` to convert an object to a string and then parses it again using `JSON.stringify`. * Pros: + No external dependencies required. + Works across most browsers and environments. * Cons: + Slow performance due to the unnecessary parsing steps. + May not work correctly with certain types of objects or data structures. **Library Explanation** The Lodash library is a popular collection of utility functions that can be used for various tasks, including copying objects. The `cloneDeep` function is specifically designed to create deep copies of objects, which makes it suitable for this benchmark. **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in the provided code. However, note that some browsers may support newer ECMAScript features (e.g., async/await, arrow functions) without explicit mention of them in the code. **Alternatives** If you're looking for alternatives to Lodash or want to explore other options, consider: 1. Built-in JavaScript methods: Some older browsers and environments may not have built-in support for modern JavaScript features. However, even older browsers like Internet Explorer can use `Array.prototype.slice()` or `Object.assign()` to create copies of objects. 2. Other cloning libraries: There are alternative libraries available that offer more efficient cloning mechanisms, such as `lodash-es` (a more modern and optimized version of Lodash) or `jsclonedeep`. 3. Custom implementation: Depending on your specific use case, you might be able to implement a custom cloning function using JavaScript's built-in data structures and operations. Keep in mind that when choosing an alternative method, consider factors such as performance, maintainability, and compatibility with different browsers and environments.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON-JSON
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Lodash cloneDeep vs structuredClone vs Json.stringify
Comments
Confirm delete:
Do you really want to delete benchmark?