Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs structured clone
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy vs structured clone
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.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, array: [{ jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }, { jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }, { jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } } ], 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);
structured clone
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
structured clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
327484.0 Ops/sec
Json Clone
515958.8 Ops/sec
recursiveDeepCopy
2759553.2 Ops/sec
structured clone
376813.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested, compared, and considered. **Benchmark Overview** The benchmark tests four different methods to create a deep copy of an object: `lodash.cloneDeep`, `JSON.parse(JSON.stringify())`, `recursiveDeepCopy`, and `structuredClone`. The test object (`MyObject`) contains nested objects and arrays with sensitive data. **Tested Options** 1. **`Lodash CloneDeep`**: This method uses the `cloneDeep()` function from Lodash, a popular utility library for JavaScript. 2. **`JSON.parse(JSON.stringify())`**: This method uses the built-in `JSON.parse()` function to parse a JSON string created by `JSON.stringify()`. Note that this method can lead to security vulnerabilities if not used carefully, as it can create objects with sensitive data. 3. **`recursiveDeepCopy`**: This is a custom implementation of a deep copy function using recursion. 4. **`structuredClone`**: This method uses the `structuredClone()` function introduced in ECMAScript 2020 (ES10), which creates a new clone of an object by recursively cloning its properties. **Comparison and Considerations** When comparing these methods, we need to consider factors such as: * Performance: How fast do each method execute? * Memory usage: What is the memory footprint of each method's output? * Security: Can each method introduce security vulnerabilities if not used correctly? * Browser support: Are all methods supported by the tested browsers (in this case, Firefox 129)? **Lodash CloneDeep** Pros: * Fast and efficient * Robust implementation with many edge cases handled Cons: * Requires an external library (Lodash) * May have a higher memory footprint due to the overhead of the library **JSON.parse(JSON.stringify())** Pros: * Lightweight, no external libraries required * Easy to implement, but may be slower than Lodash CloneDeep Cons: * Can introduce security vulnerabilities if not used carefully * Not suitable for creating deep copies of complex objects with nested arrays and objects. **recursiveDeepCopy** Pros: * Custom implementation allows for fine-grained control over the copying process * No external libraries required Cons: * Manual implementation can be error-prone and slow * May not handle all edge cases correctly **structuredClone** Pros: * New, standardized function with built-in support in modern browsers * Fast and efficient * Robust implementation with many edge cases handled Cons: * Requires a modern browser that supports ECMAScript 2020 (ES10) * May have limited support for older browsers or environments. In summary, the benchmark tests four different methods to create deep copies of objects, each with its pros and cons. The choice of method depends on factors such as performance requirements, security concerns, and compatibility with specific browsers or environments. **Other Alternatives** If none of these methods are suitable for your use case, other alternatives might include: * Using a library like Immutable.js or Ramda * Implementing a custom deep copy function using a different algorithm (e.g., JSON serialization followed by deserialization) * Using a more specialized data structure library that provides built-in support for deep copying (e.g., Redux's `createDeepCopy` function) Keep in mind that the best approach will depend on your specific requirements and constraints.
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
lodash clonedeep vs json.parse(stringify()) vs recursivecopy vs shallowcopy
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Comments
Confirm delete:
Do you really want to delete benchmark?