Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep Clone Performance - JSON vs Lodash vs Ramda vs Native1
(version: 0)
Comparing performance of:
JSON Stringfy vs Lodash cloneDeep vs Ramda Copy vs Recursive Copy vs Recursive Copy Distinct
Created:
4 years ago
by:
Guest
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.25.0/ramda.min.js"></script>
Script Preparation code:
var obj = {a: "hello", c: "test", po: 33, x: {y: 5677}, arr: [1, 2, 3, 4], anotherObj: {a: 33, str: "whazzup"}}; var myCopy = null;
Tests:
JSON Stringfy
myCopy = JSON.parse(JSON.stringify(obj));
Lodash cloneDeep
myCopy = _.cloneDeep(obj);
Ramda Copy
myCopy = R.clone(obj);
Recursive Copy
var deepCopy = function(obj) { var out = Array.isArray(obj) ? [] : {}; for (var key in obj) { var v = obj[key]; out[key] = (typeof v === 'object') ? deepCopy(v) : v; } return out; }; myCopy = deepCopy(obj);
Recursive Copy Distinct
var deepCopy = function(block) { var out = null; if (Array.isArray(block)) { out = []; for (var index = 0; index < block.length; ++index) { var subArray = block[index]; out.push((typeof subArray === 'object') ? deepCopy(subArray) : subArray); } } else { out = {}; for (var key in block) { var subObject = block[key]; out[key] = (typeof subObject === 'object') ? deepCopy(subObject) : subObject; } } return out; }; myCopy = deepCopy(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
JSON Stringfy
Lodash cloneDeep
Ramda Copy
Recursive Copy
Recursive Copy Distinct
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 world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark measures the performance of different approaches to deep cloning a complex object in JavaScript: JSON stringification, Lodash `cloneDeep`, Ramda `copy`, and two recursive copy functions. **Options Compared** 1. **JSON Stringification**: This approach converts the entire object to a JSON string using `JSON.stringify()` and then parses it back into an object using `JSON.parse()`. Since JSON strings are inherently immutable, this approach doesn't create a deep copy of the original object. 2. **Lodash `cloneDeep`**: Lodash is a popular utility library that provides various functions for working with JavaScript objects. `cloneDeep()` creates a deep copy of the input object, recursively iterating through its properties and creating new objects for each nested element. 3. **Ramda `copy`**: Ramda is another functional programming library that offers a `copy()` function to create deep copies of objects. Similar to Lodash's `cloneDeep()`, it recursively creates new objects for each nested property. 4. **Recursive Copy (Native1)**: This approach defines two recursive functions (`deepCopy()` and `deepCopy Distinct()`) to manually create a deep copy of the input object. The difference between the two functions lies in how they handle array elements versus object properties. **Pros and Cons** * **JSON Stringification**: + Pros: Simple, lightweight, and doesn't require any external libraries. + Cons: Creates an immutable JSON string that needs to be parsed back into a JavaScript object, which can lead to performance issues with large objects. * **Lodash `cloneDeep`**: + Pros: Efficiently handles complex nested objects, provides a simple API, and is widely used in the industry. + Cons: Requires including an external library (Lodash), which might increase bundle size and weight. * **Ramda `copy`**: + Pros: Similar to Lodash's `cloneDeep()`, efficient, and has a simple API. + Cons: Also requires including an external library (Ramda). * **Recursive Copy (Native1)**: + Pros: Customizable, allows for better control over the cloning process, and doesn't require any external libraries. + Cons: More complex to implement correctly, may lead to performance issues if not optimized properly. **Other Considerations** When choosing a deep clone approach, consider factors like: * Performance: If speed is critical, Lodash's `cloneDeep()` or Ramda's `copy()` might be better options due to their optimized implementations. * Code readability and maintainability: The recursive copy functions (Native1) provide more insight into the cloning process, but may require additional effort to understand and implement correctly. * Bundle size and weight: Including external libraries like Lodash or Ramda can increase your application's bundle size. **Alternatives** If you're not satisfied with the provided options or want to explore other approaches, consider: 1. **Zepto**: A lightweight alternative to jQuery that provides a `clone()` function for deep cloning objects. 2. **Immutable.js**: A library specifically designed for working with immutable data structures and deep copying objects. 3. **Manual implementation using `Array.prototype.slice()` and `Object.assign()`**: This approach involves manually iterating through the object's properties and creating new objects or arrays as needed. While straightforward, it may not be as efficient as Lodash's `cloneDeep()` or Ramda's `copy()`.
Related benchmarks:
Ramda vs. Lodash vs Fastest Clone
Deep Clone Performance - JSON vs Lodash vs Ramda vs Native
Lodash deep clone vs JSON.stringfy
Lodash cloneDeep vs JSON Clone vs freeze and get - access a value
Comments
Confirm delete:
Do you really want to delete benchmark?