Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Cloning Comparsion
(version: 0)
RFDC vs Lodash deepClone vs JSON
Comparing performance of:
Lodash vs JSON vs rfdc
Created:
2 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" } ] } ]; var cloneObject = null;
Tests:
Lodash
cloneObject = _.cloneDeep(sampleObject);
JSON
cloneObject = JSON.parse(JSON.stringify(sampleObject));
rfdc
var clone = rfdc(); cloneObject = clone(sampleObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash
JSON
rfdc
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 benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three approaches to clone an object: Lodash's `deepClone` function, JSON cloning using `JSON.parse(JSON.stringify())`, and a custom implementation called rfdc (short for "Regular Function Decomposition"). **Object Cloning Approaches** 1. **Lodash's `deepClone`**: This is a popular JavaScript library that provides utility functions for working with objects, arrays, and more. In this benchmark, Lodash's `deepClone` function is used to create a deep copy of the input object. 2. **JSON Cloning**: This approach uses the `JSON.parse(JSON.stringify())` method to clone an object by serializing it as JSON and then parsing it back into JavaScript. Since JSON only supports basic data types (e.g., numbers, strings, booleans), this approach may not work correctly for complex objects like the one in the benchmark. 3. **rfdc**: This is a custom implementation that creates a deep copy of an object by recursively traversing its properties and creating new objects to store their values. **Pros and Cons** * **Lodash's `deepClone`**: + Pros: Fast, efficient, and widely supported. + Cons: Requires including the Lodash library in your project. * **JSON Cloning**: + Pros: Lightweight and easy to understand. + Cons: May not work correctly for complex objects or when dealing with circular references. * **rfdc**: + Pros: Customizable, efficient, and doesn't require any external libraries. + Cons: Requires manual implementation of the cloning logic. **Other Considerations** * **Caching**: All three approaches can benefit from caching to improve performance. Lodash's `deepClone` has a built-in caching mechanism, while JSON cloning and rfdc would require explicit caching implementation. * **Memory Allocation**: The number of objects created during cloning affects memory allocation and garbage collection. rfdc, being a custom implementation, allows for more control over this aspect. **Benchmark Results** The latest benchmark results show that: * rfdc outperforms Lodash's `deepClone` and JSON cloning in terms of executions per second. * The order of operations can significantly impact performance: the JavaScript engine optimizes some operations (e.g., array iteration) internally, while others require explicit loops or recursive functions. Keep in mind that these results are specific to this benchmark setup and might not generalize across all scenarios.
Related benchmarks:
RFDC clone vs Lodash cloneDeep vs JSON Clone
RFDC clone vs Lodash cloneDeep vs JSON Parse
RFDC clone vs Lodash clone vs JSON Clone
RFDC clone (circles: true) vs Lodash cloneDeep vs JSON Clone
Comments
Confirm delete:
Do you really want to delete benchmark?