Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cloneDeep vs JSON stringify + parse (structured arr)
(version: 0)
Comparing performance of:
cloneDeep vs JSON
Created:
3 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 Preparation code:
var obj = [ { "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" } ] } ]
Tests:
cloneDeep
_.cloneDeep(obj);
JSON
JSON.parse(JSON.stringify(obj))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
cloneDeep
JSON
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 and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between two methods for deep cloning an object: 1. `_.cloneDeep(obj)` from Lodash 2. JSON stringification followed by parsing (`JSON.parse(JSON.stringify(obj))`) **What's Being Tested?** Both tests aim to create a deep copy of the input object `obj`, which contains nested arrays and objects. **Options Compared** The benchmark compares two methods: 1. **Deep Cloning with Lodash**: Uses the `_.cloneDeep()` function from Lodash, which is designed for deep cloning. 2. **JSON Stringification and Parsing**: Converts the object to a JSON string using `JSON.stringify(obj)` and then parses it back into an object using `JSON.parse()`. This approach relies on the default behavior of `JSON.stringify()` to create a shallow copy of the object. **Pros and Cons** 1. **Deep Cloning with Lodash**: * Pros: + Designed for deep cloning, ensuring accuracy. + Handles complex data structures (arrays, objects). * Cons: + Requires an additional library (Lodash), which may introduce overhead. 2. **JSON Stringification and Parsing**: * Pros: + Lightweight, no extra dependencies required. + Can be faster since it only involves string manipulation. * Cons: + May not work correctly for complex data structures or large objects. + Creates a shallow copy of the object, which may not be suitable in all cases. **Other Considerations** When using `JSON.stringify()` followed by `JSON.parse()`, keep in mind that: * This approach only creates a shallow copy of the object, as `JSON.stringify()` only traverses the top-level properties. * This method can fail if the input object contains circular references or other complex structures. **Special JS Feature/Syntax** This benchmark does not explicitly use any special JavaScript features or syntax. However, it relies on the behavior of `JSON.stringify()` and `JSON.parse()`, which are built-in functions in JavaScript. Now, regarding alternatives to these methods: If you need a deep clone that's more efficient than Lodash's `_.cloneDeep()` or if you prefer not to use an external library, you can consider using a custom implementation that recursively clones the object. Alternatively, you could explore other libraries or modules that provide alternative cloning functions. For example, you might look into using `immer` or ` deepcopy` from `deepcopy` module
Related benchmarks:
RFDC clone vs Lodash cloneDeep vs JSON Parse
RFDC clone vs Lodash cloneDeep vs JSON Clone vs structured clone
RFDC clone vs Lodash cloneDeep vs JSON Clone vs structuredClone
RFDC clone vs Clone vs JSON Clone
RFDC clone vs Lodash cloneDeep vs JSON Clone vs Clone
Comments
Confirm delete:
Do you really want to delete benchmark?