Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone vs freeze and destructure
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs freeze and destructure
Created:
5 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;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
freeze and destructure
Object.freeze(MyObject); myCopy = { ...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
freeze and destructure
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
1515538.0 Ops/sec
Json clone
1332804.0 Ops/sec
freeze and destructure
9985385.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided JSON benchmark. **Benchmark Overview** The test measures the performance of three different methods to create a deep copy of an object: 1. **Lodash `cloneDeep`**: A function from the popular JavaScript utility library Lodash that creates a deep clone of an object. 2. **JSON Clone**: The built-in `JSON.parse(JSON.stringify(obj))` method, which is not recommended for creating deep copies due to its limitations. 3. **freeze and destructure**: An approach that uses the `Object.freeze()` method to freeze the original object and then creates a new object by destructuring (using `{ ...MyObject }`) the frozen object. **Options Compared** The three options are compared in terms of their execution speed, measured in executions per second. **Pros and Cons of Each Approach** 1. **Lodash `cloneDeep`**: * Pros: Fast, reliable, and widely used. * Cons: Requires Lodash library to be included, which may add unnecessary overhead. 2. **JSON Clone**: * Pros: Simple and built-in, but can fail if the object contains functions or dates. * Cons: Inefficient for large objects and can lead to unexpected behavior due to its limitations. 3. **freeze and destructure**: * Pros: Fast and memory-efficient, as it only creates a shallow copy of the object. * Cons: May not be suitable for objects that contain functions or dates, as they will be preserved. **Library Used** Lodash is a popular JavaScript utility library that provides various helper functions for tasks like string manipulation, array operations, and more. In this benchmark, it's used to provide the `cloneDeep` function. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The code uses standard JavaScript syntax and object literals to create the objects being copied. **Other Alternatives** If you need to create a deep copy of an object, other alternatives to Lodash's `cloneDeep` include: * **Array.prototype.slice()**: Creates a shallow copy of an array. * **Object.assign()**: Creates a new object with properties from an existing object or array. * **lodash-es** (a modern version of Lodash): Provides similar functionality to `cloneDeep`, but may be faster and more efficient due to its optimized implementations. In summary, the benchmark provides a comparison of three methods for creating deep copies of objects: Lodash's `cloneDeep`, JSON Clone, and freeze-and-destructure. Each approach has its pros and cons, and understanding these trade-offs can help developers choose the best solution for their specific use case.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON Clone vs freeze and get - access a value
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?