Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object clone vs
(version: 2)
Comparing performance of:
Lodash cloneDeep vs Json clone vs Ramda clone vs Pvorb Clone
Created:
6 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.26.1/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);
Pvorb Clone
myCopy = clone(MyObject, false);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Ramda clone
Pvorb 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):
I'll break down the provided benchmark definition and test cases to explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The benchmark measures the performance of different cloning methods for JavaScript objects. The script `MyObject` is defined with nested properties (including a nested object `jayson`) that will be cloned using various methods. The goal is to create an exact copy of the original object, including all its properties and nested structures. **Cloning Methods** Four cloning methods are compared: 1. **Lodash cloneDeep**: Lodash's `cloneDeep` function creates a deep copy of the original object. 2. **JSON.clone**: JSON.parse(JSON.stringify(MyObject)) creates a shallow copy of the original object, but it will not preserve nested objects or arrays. 3. **Ramda.clone**: Ramda's `clone` function creates a deep copy of the original object, similar to Lodash's cloneDeep. 4. **Pvorb Clone**: Pvorb's `clone` function creates a deep copy of the original object, with some differences in behavior compared to Lodash and Ramda. **Comparison** The comparison is between these four cloning methods, which have different approaches: * **Lodash cloneDeep** and **Ramda.clone** use recursive functions to traverse the object graph and create a new copy. * **JSON.clone** relies on JSON serialization and deserialization, which only works for simple data structures like numbers, strings, booleans, arrays, and objects with no nested properties. * **Pvorb Clone** uses its own algorithm, which may differ from Lodash and Ramda's approaches. **Pros and Cons** Here are some pros and cons of each approach: 1. **Lodash cloneDeep**: * Pros: High performance, accurate for complex object graphs. * Cons: May have memory overhead due to the creation of a new object. 2. **JSON.clone**: * Pros: Simple, widely supported. * Cons: Fails for objects with nested properties or arrays, may not preserve data integrity. 3. **Ramda.clone**: * Pros: Similar performance to Lodash cloneDeep, easy to use. * Cons: May have memory overhead similar to Lodash's cloneDeep. 4. **Pvorb Clone**: * Pros: Optimized for specific use cases, possibly better performance in certain scenarios. * Cons: Behavior may differ from Lodash and Ramda's approaches, potentially affecting compatibility. **Other Alternatives** Besides the four cloning methods mentioned, there are other alternatives available: 1. **Array.prototype.slice()**: Not suitable for deep cloning, as it only creates a shallow copy of arrays. 2. **Object.assign()**: Only works for assigning properties from one object to another and does not create a new copy. 3. **for...in** loops or recursion with `JSON.stringify()`/`JSON.parse()`: These methods can be used to clone objects, but they are slower and more error-prone than the four cloning methods mentioned. Keep in mind that the choice of cloning method depends on the specific requirements of your project, such as performance, memory usage, and data integrity.
Related benchmarks:
Lodash cloneDeep vs JSON Clone vs Ramda Clone test
Lodash cloneDeep vs JSON Clone vs Obj Clone
Lodash cloneDeep vs JSON Clone vs Ramda Clone
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?