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);
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
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is a set of instructions that outlines how to prepare for the test. In this case, it defines an object `obj` with various properties (e.g., strings, numbers, arrays) and another object `anotherObj`. The goal is to create a deep clone of `obj`. **Options Compared** Four different approaches are being tested: 1. **JSON Stringification**: This involves converting the original object to a JSON string using `JSON.stringify()` and then parsing it back into an object using `JSON.parse()`. 2. **Lodash Deep Clone**: This uses the `_.clone()` function from Lodash, which is a popular utility library for functional programming in JavaScript. 3. **Object Assign**: This uses the `Object.assign()` method to create a new object by copying the properties of the original object into it. 4. **Ramda Clone**: This uses the `R.clone()` function from Ramda, another popular utility library for functional programming in JavaScript. **Pros and Cons of Each Approach** 1. **JSON Stringification**: * Pros: Simple to implement, widely supported by most browsers. * Cons: Can be slow due to the overhead of serializing and deserializing the object, may not preserve all property metadata. 2. **Lodash Deep Clone**: (Note: The benchmark actually uses `_.cloneDeep()`, not just `_`.) * Pros: Preserves all property metadata, can handle complex data structures like arrays and objects with circular references. * Cons: Requires an external library (Lodash), may have slower performance due to the overhead of function calls. 3. **Object Assign**: (Note: The benchmark actually uses `Object.assign()` directly on the object, not creating a new object.) * Pros: Fast, lightweight, and widely supported by modern browsers. * Cons: Does not preserve all property metadata, may not handle circular references well. 4. **Ramda Clone**: * Pros: Preserves all property metadata, can handle complex data structures like arrays and objects with circular references. * Cons: Requires an external library (Ramda), may have slower performance due to the overhead of function calls. **Library Descriptions** 1. **Lodash**: A popular utility library for functional programming in JavaScript, providing a wide range of functions for tasks like array manipulation, object iteration, and more. 2. **Ramda**: Another popular utility library for functional programming in JavaScript, providing a different set of functions than Lodash but with similar goals. **Special JS Features/Syntax** There are no special JS features or syntax used in this benchmark, aside from the external libraries mentioned above. **Other Alternatives** For creating deep clones, other alternatives to the ones mentioned above include: 1. **Array.prototype.slice() + Object.assign()**: Creates a shallow clone of an array by using `Array.prototype.slice()` to create a new array and then uses `Object.assign()` to copy properties from the original object. 2. **JSON.parse(JSON.stringify(obj))**: Similar to JSON stringification, but creates a deep clone instead of a shallow one. Keep in mind that these alternatives may have different trade-offs in terms of performance, memory usage, or compatibility with older browsers.
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?