Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs hand rolled vs spread vs obj.assign vs structuredClone
(version: 0)
just benchmarking things
Comparing performance of:
Lodash cloneDeep vs Json clone vs Ramda clone vs hand rolled vs spread vs obj.assign vs structuredClone
Created:
2 years ago
by:
Registered User
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 src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/clone/2.1.2/clone.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;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Ramda clone
myCopy = R.clone(MyObject);
hand rolled
const cp=(obj)=>"object" === typeof obj ? Object.fromEntries(Object.entries(obj).map(([k,v])=>[k,cp(v)])) : obj myCopy = cp(MyObject)
spread
myCopy = {...MyObject}
obj.assign
myCopy = Object.assign({}, MyObject)
structuredClone
myCopy = structuredClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Ramda clone
hand rolled
spread
obj.assign
structuredClone
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 dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided JSON represents a benchmark test case that compares six different methods for creating a deep copy of an object: 1. Lodash `cloneDeep` 2. JSON clone (`JSON.parse(JSON.stringify())`) 3. Ramda `clone` 4. Hand-rolled implementation (using `Object.fromEntries` and recursion) 5. Spread operator (`{...MyObject}`) 6. `Object.assign({}, MyObject)` Each test case is defined in the individual test cases array, which includes a benchmark definition code snippet that demonstrates how to create a deep copy of the input object using each method. **Options compared:** The comparison involves the following options: * Lodash `cloneDeep` * JSON clone (`JSON.parse(JSON.stringify())`) * Ramda `clone` * Hand-rolled implementation * Spread operator (`{...MyObject}`) * `Object.assign({}, MyObject)` **Pros and Cons of each approach:** 1. **Lodash `cloneDeep`**: Pros: * Well-tested and maintained library. * Provides a simple and efficient way to create deep copies. Cons: * Adds dependency on the Lodash library. 2. **JSON clone (`JSON.parse(JSON.stringify())`)**: Pros: * Lightweight and easy to implement. Cons: * Can fail if the input object contains functions or other non-standard values. 3. **Ramda `clone`**: Pros: * Similar to Lodash `cloneDeep`, but with a smaller dependency footprint (since Ramda is often used as a utility library). Cons: * May not be as well-tested or maintained as Lodash. 4. **Hand-rolled implementation**: Pros: * Provides a deep understanding of the underlying mechanics. Cons: * Can be error-prone and time-consuming to implement correctly. 5. **Spread operator (`{...MyObject}`)**: Pros: * Simple and concise way to create shallow copies. Cons: * Does not work for nested objects or arrays. 6. **`Object.assign({}, MyObject)`**: Pros: * Convenient and easy to use for shallow copies. Cons: * Can be slow due to the overhead of property iteration. **Libraries and their purpose:** 1. Lodash: A utility library that provides a wide range of functions for working with JavaScript data structures, including deep copying (`cloneDeep`). 2. Ramda: A functional programming library that provides a set of immutable and predictable functions for working with data. 3. Clone (JSON clone): A lightweight library that provides a simple way to create copies of JSON objects. **Special JS feature or syntax:** The benchmark test case uses the spread operator (`{...MyObject}`) to create shallow copies, which is a relatively recent addition to JavaScript. The hand-rolled implementation also uses modern JavaScript features like `Object.fromEntries` and recursive functions. **Alternatives:** Other alternatives for creating deep copies in JavaScript include: 1. **Deep copy libraries**: Other libraries that provide deep copying functionality, such as `lodash-deep` or `lodash-clonedeep`. 2. **JSON serialization/deserialization**: Using JSON serialization techniques to create a string representation of the object and then deserializing it back into an object. 3. **Array methods**: For arrays, using methods like `Array.prototype.slice()` or `Array.prototype.concat()` can be used to create shallow copies. I hope this explanation helps you understand the world of JavaScript microbenchmarks!
Related benchmarks:
Lodash cloneDeep vs JSON Clone vs Ramda Clone test
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs Pvorb Clonesdsd
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs Pvorb Cloneaaaaaaaaaaaaaaaaaaaaaaaaaa
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs JSON Clone vs Ramda Clone vs hand rolled vs spread vs obj.assign vs structured clone
Comments
Confirm delete:
Do you really want to delete benchmark?