Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep clone differencies - big records pt. 2
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Native JSON parse
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.21/lodash.min.js'></script>
Script Preparation code:
var testArray = []; for (var i = 0; i < 1e6; i++) { testArray.push({ description: 'Random description.', testNumber: 123456789, testBoolean: true, testObject: { testString: 'test string', testNumber: 12345 }, testArray: [{ myName: 'test name', myNumber: 123245 }] }) } var testCopy = null;
Tests:
Lodash cloneDeep
testCopy = _.cloneDeep(testArray);
Native JSON parse
testCopy = JSON.parse(JSON.stringify(testArray));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Native JSON parse
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Overview** The test case is designed to measure the performance of two approaches for creating deep clones of a large JSON object: native JSON parsing with `JSON.parse(JSON.stringify())` and using the Lodash library's `cloneDeep()` function. **Options Compared** 1. **Native JSON parse**: This approach uses the built-in JavaScript method `JSON.parse(JSON.stringify(obj))`, which creates a shallow clone of an object by recursively copying its properties. However, this method has some limitations: * It only works for objects that can be serialized to JSON (i.e., it doesn't work with functions, sets, or other non-serializable values). * It creates a new object reference, but the cloned object's properties are references to the original object's properties. 2. **Lodash cloneDeep**: This approach uses the Lodash library's `cloneDeep()` function, which creates a deep clone of an object by recursively copying its properties and creating new objects for each nested property. **Pros and Cons** * **Native JSON parse**: + Pros: Simple to implement, widely supported. + Cons: Limited to serializable values, creates shallow clones, may not work as expected with complex data structures. * **Lodash cloneDeep**: + Pros: Creates deep clones, handles complex data structures, and is more flexible than native JSON parsing. + Cons: Requires the Lodash library, which adds overhead. **Library Used** The Lodash library is used in this benchmark to provide the `cloneDeep()` function. Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks such as string manipulation, array manipulation, and object cloning. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The test cases only focus on the performance of the two cloning approaches. **Other Alternatives** If you need to create deep clones of objects in JavaScript, other alternatives to Lodash's `cloneDeep()` include: * Using a library like Immer or Ramda for object manipulation. * Implementing your own recursive function to clone objects. * Using a data cloning library like js-cloned or deepclone. Keep in mind that these alternatives may have different trade-offs in terms of performance, simplicity, and feature set.
Related benchmarks:
Shallow clone array of objects
Clone deep
array shallow clone comparison narrowed
array of objects shallow clone comparison narrowed
Array From vs lodash clone
Comments
Confirm delete:
Do you really want to delete benchmark?