Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy
Created:
6 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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
recursiveDeepCopy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
2827493.2 Ops/sec
Json Clone
2612263.8 Ops/sec
recursiveDeepCopy
8525141.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what is being tested. The provided JSON represents a JavaScript microbenchmark that compares three approaches for creating a deep copy of an object: `lodash.cloneDeep`, `JSON.parse(JSON.stringify())`, and a custom recursive function called `recursiveDeepCopy`. **Test Case 1: Lodash CloneDeep** * **Library:** Lodash is a popular JavaScript utility library developed by Isaac Schlueter. It provides a wide range of functions for common tasks, such as array manipulation, string manipulation, and object creation. * **Functionality:** The `cloneDeep` function creates a deep copy of an object, which means it recursively copies all properties and nested objects. * **Pros:** + Fast and efficient + Handles complex objects with ease + Part of the popular Lodash library * **Cons:** + Additional dependency on the Lodash library + May not be suitable for projects where size is a concern **Test Case 2: JSON.parse(JSON.stringify())** * **Functionality:** This approach uses the `JSON.parse()` function to parse a JSON string, which creates a deep copy of an object. The `JSON.stringify()` function is used to convert an object to a JSON string. * **Pros:** + Lightweight and small + No additional dependencies required + Easy to implement * **Cons:** + May not handle certain data types or objects correctly (e.g., functions, undefined values) + Can be slow for large objects **Test Case 3: recursiveDeepCopy** * **Functionality:** This is a custom function that recursively creates a deep copy of an object. It handles arrays and objects with nested properties. * **Pros:** + Customizable to handle specific use cases + No additional dependencies required + Fast and efficient * **Cons:** + Requires manual implementation and testing + May not be suitable for projects where code readability is a concern Other alternatives to consider: 1. **Object.assign()**: This method creates a shallow copy of an object, which means it only copies the top-level properties. 2. **Array.prototype.slice()**: This method creates a shallow copy of an array by returning a new array with references to the original elements. 3. **for...in loops** or **JSON.parse()` with **JSON.parse(JSON.stringify())** for more complex object structures. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case, performance requirements, and code readability concerns.
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
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Comments
Confirm delete:
Do you really want to delete benchmark?