Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs deepFreeze vs deepClone vs JSON.parse(JSON.stringify())
(version: 0)
Comparing performance of:
Lodash cloneDeep vs custom deepFreeze() vs custom deepClone() vs JSON
Created:
4 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, array: [1,2,3,4,5,6,7,8,9,0], nestedObject: { a: [{x: 1}, {x: 1}, {x: 1}], b: [{x: 1}, {x: 1}, {x: 1}], c: [{x: 1}, {x: 1}, {x: 1}] }, 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 deepFreeze(x) { if (!x || typeof x !== 'object') { return x; } if (Array.isArray(x)) { const y = []; for (let i = 0; i < x.length; i++) { y.push(deepFreeze(x[i])); } return Object.freeze(y); } const y = {}; for (const key in x) { if (x.hasOwnProperty(key)) { y[key] = deepFreeze(x[key]); } } return Object.freeze(y); } function deepClone(x) { if (!x || typeof x !== 'object') { return x; } if (Array.isArray(x)) { const y = []; for (let i = 0; i < x.length; i++) { y.push(deepClone(x[i])); } return y; } const y = {}; for (const key in x) { if (x.hasOwnProperty(key)) { y[key] = deepClone(x[key]); } } return y; }
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
custom deepFreeze()
myCopy = deepFreeze(MyObject)
custom deepClone()
myCopy = deepClone(MyObject);
JSON
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
custom deepFreeze()
custom deepClone()
JSON
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 YaBrowser/25.6.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
304688.9 Ops/sec
custom deepFreeze()
1304006.6 Ops/sec
custom deepClone()
2087775.0 Ops/sec
JSON
601802.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided JSON represents a benchmark test for comparing different approaches to create a deep copy of an object in JavaScript: `Lodash cloneDeep`, `custom deepFreeze()`, `custom deepClone()`, and `JSON.parse(JSON.stringify())`. The test measures the execution speed (in executions per second) of each approach on various browsers and devices. **Options Compared** The benchmark compares four options: 1. **Lodash cloneDeep**: A popular JavaScript library that provides a function for creating a deep copy of an object. 2. **custom deepFreeze()**: A custom implementation of the `deepFreeze()` function, which is similar to `Object.freeze()`. 3. **custom deepClone()**: A custom implementation of the `deepClone()` function, which is similar to `JSON.parse(JSON.stringify())`. 4. **JSON.parse(JSON.stringify())**: A built-in JavaScript method for creating a deep copy of an object. **Pros and Cons** Here's a brief overview of each approach: 1. **Lodash cloneDeep**: * Pros: Fast and efficient, widely used and well-tested. * Cons: Requires including the Lodash library in your project. 2. **custom deepFreeze()**: * Pros: Custom implementation, can be optimized for specific use cases. * Cons: Requires manual implementation and may have performance overhead due to its custom nature. 3. **custom deepClone()**: * Pros: Similar to `JSON.parse(JSON.stringify())`, easy to implement. * Cons: May not work correctly with all data types or nested structures, and can be slow for large objects. 4. **JSON.parse(JSON.stringify())**: * Pros: Built-in JavaScript method, works correctly with most data types. * Cons: Can be slower than custom implementations due to the overhead of parsing JSON. **Library and Purpose** * `lodash.js`: A popular JavaScript utility library that provides various functions for tasks like string manipulation, array manipulation, and more. In this case, it's used for creating a deep copy of an object using `cloneDeep()`. * `JSON.parse(JSON.stringify())`: A built-in JavaScript method that creates a deep copy of an object by parsing the object as JSON and then parsing the resulting JSON back into an object. **Special JS Features or Syntax** There are no specific JavaScript features or syntax mentioned in this benchmark. However, it's worth noting that some browsers may have different implementations of `Object.freeze()` or other related methods that could affect the performance of custom deepFreeze(). **Alternatives** If you're interested in exploring alternative approaches to creating a deep copy of an object, here are some options: * **immer.js**: A library that provides a more functional approach to mutating objects, which can be useful for creating a deep copy. * **fast-json-stable-stringify**: A lightweight library that provides a fast and efficient way to serialize and deserialize JSON objects. * **custom implementation using `Map` or `WeakMap`**: You can create a deep copy of an object by recursively iterating over its properties and creating new instances of the same data type (e.g., `Map` for objects) or mapping the original values to new ones.
Related benchmarks:
Lodash cloneDeep vs Json clone (deeply nested)
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
Lodash cloneDeep vs freeze vs monokee deepClone
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Comments
Confirm delete:
Do you really want to delete benchmark?