Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DeepClone vs JSON Stringify/Parse
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Stringify
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 o = { a: { b: 1, c: 2, d: 3, j: { k: [1,2,3], l: [4,5,6] }, }, e: [1,2,3,4,5,6], f: 1, g: { h: 1, } }
Tests:
Lodash cloneDeep
const a = _.cloneDeep(o)
Stringify
const a = JSON.parse(JSON.stringify(o))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
886507.7 Ops/sec
Stringify
2013257.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of two approaches: using Lodash's `cloneDeep` function to create a deep copy of an object, and using JSON stringification and parsing to achieve the same result. **Options Compared** Two options are compared: 1. **Lodash cloneDeep**: This method uses Lodash's `cloneDeep` function to create a deep copy of the input object `o`. Lodash is a utility library that provides various functions for tasks like array manipulation, object creation, and more. 2. **JSON Stringify/Parse**: This approach involves stringifying (converting) the entire input object `o` into a JSON string using `JSON.stringify(o)`, and then parsing (converting back to an object) the resulting string using `JSON.parse(JSON.stringify(o))`. This method is often used for serializing and deserializing data in JavaScript applications. **Pros and Cons of Each Approach** 1. **Lodash cloneDeep**: * Pros: + More efficient, as it only clones a subset of the object's properties. + Less memory-intensive, as it doesn't create an entire new JSON string. * Cons: + Requires Lodash to be included in the script, which may add unnecessary overhead. 2. **JSON Stringify/Parse**: * Pros: + Simple and straightforward implementation. + No additional dependencies are required. * Cons: + Inefficient, as it creates an entire new JSON string, which can lead to increased memory usage. + May not handle cyclic references (i.e., objects referencing themselves) correctly. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides various functions for tasks like array manipulation, object creation, and more. In this benchmark, Lodash's `cloneDeep` function is used to create a deep copy of an object. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on comparing the performance of two different approaches: using a library (Lodash) versus a built-in method (`JSON.stringify()` and `JSON.parse()`). **Other Alternatives** If you're looking for alternative methods to create deep copies of objects, some other options include: 1. **Spread operator**: You can use the spread operator (`{...o}`) to create a shallow copy of an object. 2. **Object.assign()**: You can use `Object.assign()` to create a shallow copy of an object. 3. **Array.prototype.slice()**: You can use `Array.prototype.slice()` to create a shallow copy of an array. Keep in mind that these alternatives may not provide the same level of functionality as Lodash's `cloneDeep` function, which is designed specifically for creating deep copies of objects and arrays.
Related benchmarks:
Object cloning with Lodash clone vs cloneDeep vs merge vs ES6 object spread vs ES6 Object.assign vs JSON.parse(JSON.stringify())
Object cloning with Lodash cloneDeep vs ES6 object spread vs JSON.stringify
Object cloning with Lodash clone vs cloneDeep vs merge vs structuredClone vs JSON.parse(JSON.stringify(o))
Object cloning with Lodash clone vs cloneDeep vs merge vs structuredClone vs JSON.parse(JSON.stringify(o)) 2
Comments
Confirm delete:
Do you really want to delete benchmark?