Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Benchmark cloning methods
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Object create
Created:
7 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:
var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, myDate: new Date(), myDate2: new Date(), myDate3: new Date(), myDate4: new Date(), myDate5: new Date(), myDate6: new Date(), myDate7: new Date(), myDate8: new Date(), myDate9: new Date(), myDate10: new Date(), jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson2: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson3: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson4: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson5: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson6: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson7: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson8: { 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);
Object create
myCopy = Object.create( Object.getPrototypeOf(MyObject), Object.getOwnPropertyDescriptors(MyObject))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Object create
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark is defined by two test cases: 1. **Lodash cloneDeep**: This test case uses the `cloneDeep` function from the Lodash library to create a deep copy of the `MyObject` object. 2. **Object create**: This test case creates a new object using `Object.create()` and sets its prototype to the prototype of `MyObject`, effectively creating a shallow copy of `MyObject`. **Script Preparation Code** The script preparation code defines the `MyObject` object with various properties, including `description`, `myNumber`, `myBoolean`, `myDate` (10 instances), and `jayson` objects (5 instances). These properties will be used to test the cloning behavior. **Html Preparation Code** The HTML preparation code includes a script tag that loads the Lodash library version 4.17.5, which is used by the first test case (`Lodash cloneDeep`). **What's being tested?** The benchmark tests two different approaches to create a copy of an object: 1. **Shallow Copy using Object.create()**: This approach creates a new object and sets its prototype to the prototype of the original object, effectively copying only the own enumerable properties. 2. **Deep Copy using Lodash cloneDeep**: This approach uses the `cloneDeep` function from Lodash to create a deep copy of the entire object graph, including all nested objects and arrays. **Pros and Cons** 1. **Shallow Copy using Object.create()**: * Pros: Efficient, lightweight, and easy to implement. * Cons: May not work correctly with complex objects or objects that have circular references. 2. **Deep Copy using Lodash cloneDeep**: * Pros: Can handle complex objects and circular references, but may be slower and more memory-intensive due to the overhead of JSON serialization. * Cons: Requires an additional library (Lodash) and can add unnecessary complexity. **Other Considerations** * The benchmark uses multiple instances of `myDate` and `jayson` objects to test the cloning behavior with arrays and nested objects. * The use of `Object.create()` may not be supported in older browsers or environments that don't support modern JavaScript features. * The Lodash library is widely used, but its availability and versioning can vary depending on the environment. **Alternatives** If you need to create a copy of an object without using Lodash, you could use other libraries like `json-stringify-safe` for JSON serialization or implement your own cloning algorithm. Alternatively, you could use built-in functions like `Array.prototype.slice()` and `Object.assign()` to create shallow copies. Keep in mind that the choice of cloning approach depends on the specific requirements and constraints of your project.
Related benchmarks:
Lodash clone vs cloneDeep
Benchmark cloning methodsasdasd
Lodash cloneDeep vs JSON Clone with date
JSON.stringify vs structuredClone vs cloneLineageData vs cloneDeep
Comments
Confirm delete:
Do you really want to delete benchmark?