Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RFDC clone vs Lodash cloneDeep vs JSON Parse
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs RFDC copy
Created:
5 years ago
by:
Guest
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:
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 sampleString = JSON.stringify(sampleObject) var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(sampleObject);
Json clone
myCopy = JSON.parse(sampleString);
RFDC copy
var clone = rfdc(); myCopy = clone(sampleObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
RFDC copy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
24560.5 Ops/sec
Json clone
74752.7 Ops/sec
RFDC copy
86970.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an explanation of the benchmark, its options, pros and cons, and other considerations. **Benchmark Overview** The benchmark compares the performance of three different methods for cloning or copying a JavaScript object: 1. Lodash's `cloneDeep` function 2. The `JSON.parse` method (which essentially creates a shallow copy by parsing the original JSON string) 3. A custom implementation called RFDC (`rfdc`) which clones the entire object graph. **Option Comparison** ### Clone with Lodash (`_.cloneDeep`) Pros: * Well-tested and widely used library * Provides a functional programming style for cloning objects Cons: * May introduce additional dependencies (although in this case, it's just Lodash) * Can be slower than native implementations due to the overhead of a library function ### Clone with `JSON.parse` (`JSON.clone`) Pros: * Native JavaScript method * Fast and lightweight implementation Cons: * Creates only a shallow copy (i.e., it doesn't recursively clone nested objects) * May not work correctly if the original object contains functions, undefined values, or other complex data types that can't be serialized. ### Clone with RFDC (`rfdc`) Pros: * Custom implementation provides full control over cloning logic * Can handle more complex data types than `JSON.parse` Cons: * Less tested and maintained compared to Lodash's library functions * May have performance overhead due to custom implementation **Other Considerations** * The benchmark only measures the time taken by each method for executing a single iteration of the cloning process. To get a comprehensive picture, additional tests might be necessary. * The `JSON.parse` method assumes that the original object is a valid JSON string. If the input data is not well-formed, this approach will throw an error. **Library Explanation** In this benchmark, Lodash's `cloneDeep` function and the RFDC custom implementation are used for cloning objects. * **Lodash's cloneDeep**: This method takes an object as input and returns a deep copy of it. It recursively clones all nested objects, arrays, and even functions. * **RFDC**: The RFDC implementation is a simple custom function that uses recursion to clone the entire object graph, including nested objects and arrays. **Benchmark Results** The latest benchmark results show that: 1. Lodash's `cloneDeep` provides the slowest execution time (approximately 0.25 seconds per iteration) due to its overhead from being a library function. 2. The `JSON.parse` method has an intermediate performance, taking around 0.05-0.15 seconds per iteration. 3. RFDC provides the fastest execution time (approximately 0.01-0.02 seconds per iteration), likely because it's a custom implementation optimized for cloning object graphs. Keep in mind that these results might change depending on the input data and specific use cases. It's essential to consider factors like performance, code readability, and maintainability when choosing an approach for your specific needs.
Related benchmarks:
RFDC clone vs Lodash cloneDeep vs JSON Clone
RFDC clone vs Lodash clone vs JSON Clone
RFDC clone vs clone-deep clone vs Lodash cloneDeep vs JSON Clone
Object Cloning Comparsion
Comments
Confirm delete:
Do you really want to delete benchmark?