Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs structuredClone 1234
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy vs structuredClone
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; function recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; }
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(MyObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
recursiveDeepCopy
myCopy = recursiveDeepCopy(MyObject);
structuredClone
myCopy = structuredClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
recursiveDeepCopy
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The test suite is designed to compare the performance of four different methods for creating a deep copy of an object in JavaScript: 1. `lodash.cloneDeep()` 2. `JSON.parse(JSON.stringify())` 3. `recursiveDeepCopy()` (a custom implementation) 4. `structuredClone()` (a new method introduced in ECMAScript 2020) **Comparison of Methods** Each test case uses a similar input object, `MyObject`, which contains nested objects and arrays. 1. **`lodash.cloneDeep()`**: This method is part of the popular JavaScript utility library, Lodash. It recursively creates a deep copy of the original object. 2. **`JSON.parse(JSON.stringify())`**: This method serializes the input object to a JSON string, then parses it back into an object. While this works for simple objects, it can lead to issues with circular references and other complex data structures. 3. **`recursiveDeepCopy()`**: This is a custom implementation of a recursive deep copy function. It handles arrays and objects with nested properties correctly but may be less efficient than the others due to its bespoke nature. 4. **`structuredClone()`**: This method was introduced in ECMAScript 2020 as a new way to create deep copies of objects. It's designed to be more efficient and reliable than traditional methods like `JSON.parse(JSON.stringify())`. **Pros/Cons of Each Approach** 1. **`lodash.cloneDeep()`** * Pros: Robust, widely supported, and well-tested. * Cons: Requires an additional library dependency. 2. **`JSON.parse(JSON.stringify())`** * Pros: Simple to implement, works for simple objects. * Cons: Fails with circular references, can lead to incorrect results. 3. **`recursiveDeepCopy()`** * Pros: Custom implementation allows for fine-tuning performance. * Cons: May be less efficient than others, requires bespoke maintenance. 4. **`structuredClone()`** * Pros: New method designed for deep copying, more efficient and reliable. * Cons: New feature, may not be supported in older browsers or environments. **Library Used** The `lodash.cloneDeep()` method is part of the Lodash library, which provides a wide range of utility functions for JavaScript development. Lodash is widely used and well-maintained, making it a popular choice for many developers. **Special JS Feature/Syntax** None mentioned in this benchmark. **Alternatives** If you need to create deep copies of objects in JavaScript, other alternatives to `structuredClone()` include: * Using `JSON.parse(JSON.stringify())` (with caution and awareness of its limitations). * Implementing a custom recursive deep copy function like `recursiveDeepCopy()`. * Using other libraries or modules that provide similar functionality. Keep in mind that the choice of method depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone 2
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Comments
Confirm delete:
Do you really want to delete benchmark?