Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.stringify()
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone
Created:
2 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.stringify(MyObject)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
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/605.1.15 (KHTML, like Gecko) Version/26.0 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
5376340.5 Ops/sec
Json Clone
10875725.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents two individual test cases for measuring the performance of cloning an object in JavaScript. The first test case, "Json Clone", tests the `JSON.stringify()` method to create a clone of the object `MyObject`. The second test case, "Lodash CloneDeep", uses the `_cloneDeep()` function from the Lodash library to create a deep copy of `MyObject`. **What options are being compared?** The two options being compared are: 1. **JSON.stringify()**: This method converts a JavaScript value to a JSON string, which can be used to clone an object. 2. **Lodash _cloneDeep() function**: This function creates a deep copy of an object. **Pros and Cons of each approach:** * `JSON.stringify()`: + Pros: Simple to implement, widely supported. + Cons: Not designed for creating deep copies; may not preserve all properties or data types; can be slower than other methods due to the overhead of converting to a string. * Lodash `_cloneDeep()` function: + Pros: Designed specifically for creating deep copies; preserves most properties and data types; generally faster than `JSON.stringify()`. + Cons: Requires importing an additional library (Lodash); may have slightly higher memory usage. **Library used in the test case** The test case uses the Lodash library, which is a popular utility library for JavaScript. The `_cloneDeep()` function is part of the Lodash collection and provides a simple way to create deep copies of objects. **Special JS feature or syntax** None are mentioned in this specific benchmark definition. **Benchmark preparation code explanation** The script preparation code defines an object `MyObject` with various properties, including a nested object `jayson`. The `recursiveDeepCopy()` function is defined to recursively clone the object. The Html Preparation Code includes a link to the Lodash library, which is required for the `_cloneDeep()` function. **Other alternatives** Some alternative methods for creating deep copies of objects in JavaScript include: * Using the spread operator (`{...o}`) or `Object.assign()` * Implementing a custom recursive cloning function * Using a library like Immutable.js However, these methods may not provide the same level of performance or property preservation as the Lodash `_cloneDeep()` function.
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
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Comments
Confirm delete:
Do you really want to delete benchmark?