Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deep clone JSON
(version: 0)
Comparing performance of:
json stringify vs lodash deep clone vs Object.assign vs Ramda clone
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> <script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Tests:
json stringify
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = JSON.parse(JSON.stringify(obj));
lodash deep clone
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = _.clone(obj, true);
Object.assign
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = Object.assign({}, obj);
Ramda clone
var obj = {a: "hello", c: "test", po: 33, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var obj2 = R.clone(obj, true);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
json stringify
lodash deep clone
Object.assign
Ramda clone
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
gemma2:9b
, generated one year ago):
This benchmark compares different methods for creating a deep copy of a JavaScript object. **Options Compared:** * **`JSON.stringify()` and `JSON.parse()`**: This method serializes the object into a JSON string, then parses it back into an object. While simple, it can be inefficient for large or complex objects. * **`lodash.clone(obj, true)`**: Lodash is a popular JavaScript utility library. Its `clone()` function creates a deep copy of an object, ensuring that nested objects are also copied recursively. * **`Object.assign({}, obj)`**: This method creates a new object and copies the properties of the original object into it. However, it doesn't create deep copies by default; nested objects will reference the originals. * **`R.clone(obj, true)`**: Ramda is another functional programming library for JavaScript. Its `clone()` function, similar to Lodash's, creates a deep copy of an object. **Pros and Cons:** | Method | Pros | Cons | |-----------------|----------------------------------------------------|---------------------------------------------------------| | JSON.stringify | Simple, widely supported | Can be slow for large objects, doesn't preserve functions | | Lodash | Fast, reliable deep cloning | Requires an external library | | Object.assign | Built-in, concise | Shallow copy by default, might modify nested objects | | Ramda | Functional programming paradigm, reliable deep cloning | Requires an external library | **Other Considerations:** * **Performance**: The benchmark results show that `lodash.clone` is the fastest method for this specific use case. * **Library Dependency**: Using Lodash or Ramda adds a dependency to your project. * **Readability**: `Object.assign` might be more readable for simple cases, but it's easy to accidentally create shallow copies. **Alternatives:** Besides the methods listed in the benchmark, other options exist: * **Manually Copying Objects**: You could write your own recursive function to copy objects deeply. This gives you complete control but can be more complex. * **`structuredClone()`**: A newer API (available in modern browsers) that provides efficient deep cloning for most objects. Let me know if you have any other questions about this benchmark or JavaScript object cloning!
Related benchmarks:
Deep Clone Performance - JSON vs Lodash vs Ramda vs Native
lodash includes vs ramda includes
lodash merge vs deepmerge vs ramda
Lodash vs Ramda fromPairs
Deep clone: lodash vs ramda
Comments
Confirm delete:
Do you really want to delete benchmark?