Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone vs Bitfish Simple Clone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs Bitfish Simple Clone
Created:
5 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 Preparation code:
function forEach(array, iteratee) { let index = -1; const length = array.length; while (++index < length) { iteratee(array[index], index); } return array; } function clone(target, map = new WeakMap()) { if (typeof target === 'object') { const isArray = Array.isArray(target); let cloneTarget = isArray ? [] : {}; if (map.get(target)) { return map.get(target); } map.set(target, cloneTarget); if (isArray) { forEach(target, (value, index) => { cloneTarget[index] = value; }) } else { forEach(Object.keys(target), (key, index) => { cloneTarget[key] = target[key]; }) } return cloneTarget; } else { return target; } } var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Bitfish Simple Clone
myCopy = clone(MyObject)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Bitfish Simple Clone
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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three different approaches to cloning an object: `Lodash cloneDeep`, `JSON Clone`, and `Bitfish Simple Clone`. The cloned objects are created using the original `MyObject` definition, which includes various data types such as numbers, booleans, and nested objects. **Cloning Approaches** 1. **Lodash cloneDeep**: Lodash is a popular JavaScript utility library that provides various functions for tasks like cloning, merging, and more. The `cloneDeep` function creates a deep copy of an object, which means it recursively clones all nested properties. 2. **JSON Clone**: This approach uses the built-in `JSON.parse(JSON.stringify(obj))` method to create a clone of the input object. While this works for simple objects, it can lead to issues when dealing with complex data structures or functions as values. 3. **Bitfish Simple Clone**: The `clone` function provided in the benchmark definition creates a shallow copy of an object, which means only the top-level properties are cloned, and nested properties are referenced. **Pros and Cons** 1. **Lodash cloneDeep**: * Pros: Recursively clones all nested properties, preserving data integrity. * Cons: Requires Lodash to be included in the benchmark, which can add overhead. 2. **JSON Clone**: * Pros: Simple to implement, no additional dependencies required. * Cons: Can lead to issues with complex data structures or functions as values. 3. **Bitfish Simple Clone**: * Pros: Lightweight, easy to understand. * Cons: Only clones top-level properties, leading to shared references for nested properties. **Library and Special JS Features** 1. **Lodash**: A popular JavaScript utility library that provides various functions for tasks like cloning, merging, and more. 2. **JSON.parse(JSON.stringify(obj))**: A built-in method in JavaScript that creates a clone of an object by parsing the original object as a JSON string. **Other Considerations** * The benchmark uses a simple `MyObject` definition, which is easy to understand and clone. Real-world scenarios may require more complex data structures or handling specific edge cases. * The benchmark assumes a desktop browser environment, which might not accurately represent mobile or IoT device usage patterns. * The benchmark only measures the execution time of each cloning approach, without considering factors like memory usage, garbage collection, or thread safety. **Alternatives** If you're looking for alternative cloning libraries or approaches, consider: 1. **Immer**: A lightweight library for creating immutable data structures and cloning objects. 2. **Pick**: A utility function that allows selecting specific properties from an object to clone. 3. **Object.assign()**: A method for cloning objects by assigning properties from the original object. Keep in mind that each alternative has its pros and cons, and you should evaluate them based on your specific use case and requirements.
Related benchmarks:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign()
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?