Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RFDC clone vs Lodash cloneDeep vs JSON Clone vs recursive deep copy 3
(version: 2)
Comparing performance of:
Lodash cloneDeep vs Json clone vs RFDC copy vs Recursive deep copy
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/rfdc@1.1.4/index.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
Script Preparation code:
function recursiveDeepCopy(obj) { return Object.keys(obj).reduce((v, d) => Object.assign(v, { [d]: (obj[d].constructor === Object) ? recursiveDeepCopy(obj[d]) : obj[d] }), {}); } var sampleObject = [{ "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [{ "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [{ "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0002", "type": "donut", "name": "Raised", "ppu": 0.55, "batters": { "batter": [{ "id": "1001", "type": "Regular" }] }, "topping": [{ "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0003", "type": "donut", "name": "Old Fashioned", "ppu": 0.55, "batters": { "batter": [{ "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" } ] }, "topping": [{ "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] } ]; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(sampleObject);
Json clone
myCopy = JSON.parse(JSON.stringify(sampleObject));
RFDC copy
var clone = rfdc({circles: false}); myCopy = clone(sampleObject);
Recursive deep copy
myCopy = recursiveDeepCopy(sampleObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
RFDC copy
Recursive deep copy
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 benchmark and explain what's being tested, compared options, pros and cons, library usage, special JavaScript features, and alternative approaches. **Benchmark Overview** The benchmark compares the performance of four methods to create a deep copy of an object: 1. `_.cloneDeep` from Lodash 2. `JSON.parse(JSON.stringify)` (a built-in method for JSON cloning) 3. `rfdc` (a custom library for recursive deep copying) 4. A custom recursive function (`recursiveDeepCopy`) implemented by the user **Comparison of Methods** Here's a brief overview of each method: 1. **Lodash cloneDeep**: Lodash provides a convenient and efficient way to create deep copies of objects using `_.cloneDeep`. This method is part of the Lodash library, which is widely used for functional programming tasks. 2. **JSON.parse(JSON.stringify)**: This built-in JavaScript method uses JSON serialization and deserialization to create a deep copy of an object. While it works, this method can lead to unexpected behavior if the original object contains functions or undefined values. 3. **rfdc**: The `rfdc` library provides a recursive function for deep copying objects. It's designed to handle complex data structures with nested objects and arrays. 4. **Recursive Deep Copy (custom)**: The user-implemented `recursiveDeepCopy` function uses recursion to create a deep copy of the input object. **Pros and Cons** Here are some pros and cons for each method: 1. **Lodash cloneDeep**: * Pros: Efficient, easy to use, and widely supported. * Cons: Requires an additional dependency (Lodash). 2. **JSON.parse(JSON.stringify)**: * Pros: Lightweight, easy to understand. * Cons: Can lead to unexpected behavior if the original object contains functions or undefined values. 3. **rfdc**: * Pros: Handles complex data structures well, flexible configuration options. * Cons: Requires an additional dependency (the `rfdc` library). 4. **Recursive Deep Copy (custom)**: * Pros: Customizable, no dependencies required. * Cons: More code to write and maintain. **Library Usage** The benchmark uses the following libraries: 1. Lodash (`_.cloneDeep`) 2. `rfdc` 3. No additional libraries are used for HTML preparation or JavaScript execution. **Special JavaScript Features** None of the methods rely on special JavaScript features beyond standard language support. **Alternative Approaches** Other approaches to create deep copies include: 1. Using a library like `lodash-es` (a smaller, ES6-only version of Lodash) 2. Implementing a custom recursive function using `let rec = f => { ... }` 3. Using a library like `immer` for immutable data structures 4. Creating a custom shallow copy method and then recursively copying nested objects Keep in mind that these alternative approaches may not offer the same level of performance, convenience, or robustness as the methods listed in the benchmark. In conclusion, the benchmark provides a comprehensive comparison of four methods to create deep copies of objects. Each method has its pros and cons, and understanding these differences can help developers choose the most suitable approach for their specific use case.
Related benchmarks:
RFDC clone vs clone-deep clone vs Lodash cloneDeep vs JSON Clone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone 2
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?