Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
CloneDeep or clone
(version: 0)
Comparing performance of:
CloneDeep vs Clone
Created:
one year 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, 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:
CloneDeep
myCopy = _.cloneDeep(MyObject);
Clone
myCopy = _.clone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
CloneDeep
Clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
CloneDeep
1129603.2 Ops/sec
Clone
2489040.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The MeasureThat.net benchmark compares two methods for creating a deep copy of an object in JavaScript: `_.cloneDeep` from Lodash and plain old assignment (`=`). The benchmark creates a test object `MyObject` with nested properties, including a property named "jayson" that contains string values. This object is then copied using both methods. **Script Preparation Code** The script preparation code defines the `MyObject` test object: ```javascript 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...' } }; ``` **Html Preparation Code** The HTML preparation code includes the Lodash library: ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> ``` **Test Cases** There are two individual test cases: 1. **CloneDeep** ```javascript myCopy = _.cloneDeep(MyObject); ``` This method creates a deep copy of `MyObject` using the `_cloneDeep` function from Lodash. 2. **Clone** ```javascript myCopy = MyObject; ``` This method simply assigns the original object to a new variable, without creating a copy. **Comparison and Pros/Cons** The benchmark is comparing two approaches: 1. **_.cloneDeep(MyObject)**: * Creates a deep copy of `MyObject` using Lodash. * Pros: Provides a robust and efficient way to create a deep copy, handling complex object structures. * Cons: Requires the use of an external library (Lodash). 2. **MyObject = MyObject**: * Simply assigns the original object to a new variable. * Pros: Fast and lightweight, with no additional overhead. * Cons: Does not create a true copy of the object; instead, it creates a shallow reference. In general, if you need to perform complex operations on copied objects or want to ensure that modifications do not affect the original object, `_.cloneDeep` is a good choice. However, if you're working with simple objects and performance is critical, the plain assignment approach may be sufficient. **Lodash Library** The Lodash library provides various utility functions for JavaScript development. In this case, `_cloneDeep` is used to create a deep copy of an object. Other alternatives to `_.cloneDeep` include: * The native `Object.assign()` method with the `{deep: true}` option. * A custom implementation using recursion or iteration. Keep in mind that these alternatives may not provide the same level of convenience and robustness as Lodash's `_cloneDeep` function.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs clone
Lodash (4.17.11) cloneDeep vs JSON Clone
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON Clone vs Lodash clone
Comments
Confirm delete:
Do you really want to delete benchmark?