Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy heavy
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy
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, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', x: { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, bsae64String: 'abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123', jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses 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 year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0
Browser/OS:
Firefox 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
699873.0 Ops/sec
Json Clone
383450.9 Ops/sec
recursiveDeepCopy
4960478.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided benchmark. The test aims to compare the performance of three different methods for creating a deep copy of an object: 1. **Lodash `cloneDeep`**: This method uses a recursive approach to create a deep copy of an object. 2. **`JSON.parse(JSON.stringify())`**: This method uses the `Stringify()` and `Parse()` methods of the JSON object to create a deep copy of an object. 3. **`recursiveDeepCopy`**: This is a custom implementation of a recursive function that creates a deep copy of an object. **Options Comparison:** * The three options are compared in terms of their performance, which is measured by the number of executions per second (ExecutionsPerSecond). * Each option has its own strengths and weaknesses: * Lodash `cloneDeep`: This method is well-tested and optimized for deep copying objects. However, it may not be suitable for very large objects due to memory constraints. * `JSON.parse(JSON.stringify())`: This method can work with any JSON-serializable object, but it's not the most efficient way to create a deep copy, especially for complex objects. * `recursiveDeepCopy`: This custom implementation provides more control over the copying process, but it may require more code and maintenance. **Pros and Cons:** * Lodash `cloneDeep`: * Pros: Optimized, tested, and widely adopted; suitable for most use cases. * Cons: May not work well with very large objects due to memory constraints. * `JSON.parse(JSON.stringify())`: * Pros: Works with any JSON-serializable object; simple implementation. * Cons: Not optimized for performance, may not work correctly with complex objects, and can be slower than other methods. * `recursiveDeepCopy`: * Pros: Provides more control over the copying process; can handle complex objects more effectively. * Cons: Requires more code and maintenance; may not be suitable for all use cases. **Other Considerations:** * Memory usage: Creating a deep copy of an object can be memory-intensive, especially if the object contains many nested properties. Optimal solutions should consider this factor to avoid excessive memory usage. * Performance: The performance difference between these methods can vary depending on the size and complexity of the objects being copied. **Alternative Methods:** Some other alternatives for creating deep copies of objects include: 1. **Object.assign()**: This method creates a shallow copy of an object, which may not be suitable for all use cases. 2. **Array.prototype.slice()**: This method creates a shallow copy of an array, similar to `Object.assign()`. 3. **For...of loops**: You can use For...of loops in combination with Object.create() and Array.from() to create a deep copy of objects and arrays. Keep in mind that these alternatives may not provide the same level of performance or control as Lodash's `cloneDeep` method, and some might require more code and maintenance.
Related benchmarks:
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
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a simple and a more deep test
Comments
Confirm delete:
Do you really want to delete benchmark?