Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test123213
(version: 0)
Comparing performance of:
lodash vs Json string vs deepCopy vs angular Copy vs infoDeepCopyWithNullCheck
Created:
7 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="//cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.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...' }, functioada:function(){alert("testtt")} }; var myCopy = null; function deepCopy(object) { // if not array or object or is null return self if (typeof object !== 'object' || object == null) { return object; } var newObject; // handle case: array if (object instanceof Array) { newObject = []; for (var index = 0, length = object.length; index < length; index++) { newObject[index] = deepCopy(object[index]); } return newObject; } // handle case: object newObject = {}; for (var property in object) { if (object.hasOwnProperty(property)) { newObject[property] = deepCopy(object[property]); } } return newObject; } function infoDeepCopyWithNullCheck(copyItem) { if ($.isPlainObject(copyItem)) { return $.extend(true, {}, copyItem); } return _.cloneDeep(copyItem); }
Tests:
lodash
myCopy = _.cloneDeep(MyObject);
Json string
myCopy = JSON.parse(JSON.stringify(MyObject));
deepCopy
myCopy = deepCopy(MyObject);
angular Copy
myCopy = angular.copy(MyObject);
infoDeepCopyWithNullCheck
myCopy = infoDeepCopyWithNullCheck(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
lodash
Json string
deepCopy
angular Copy
infoDeepCopyWithNullCheck
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 benchmarking scenario. **Benchmark Definition JSON** The provided JSON represents a benchmark definition, which includes: 1. **Script Preparation Code**: This code sets up an object `MyObject` with various properties, including arrays and nested objects. It also defines a `deepCopy` function. 2. **Html Preparation Code**: These scripts load external libraries: * jQuery * Lodash (a utility library) * AngularJS **Individual Test Cases** Each test case consists of a single line of code that creates a copy of the `MyObject`. The tests compare different approaches: 1. **`myCopy = _.cloneDeep(MyObject);`**: Uses Lodash's `cloneDeep` function to create a deep copy of `MyObject`. 2. **`myCopy = JSON.parse(JSON.stringify(MyObject));`**: Uses JSON parsing and stringification to create a deep copy of `MyObject`. This approach is not recommended, as it can lead to issues with circular references. 3. **`myCopy = deepCopy(MyObject);`**: Defines the custom `deepCopy` function (see Script Preparation Code) to create a deep copy of `MyObject`. 4. **`myCopy = angular.copy(MyObject);`**: Uses AngularJS's `copy` function to create a shallow copy of `MyObject`. Note that this is not suitable for creating a deep copy. 5. **`myCopy = infoDeepCopyWithNullCheck(MyObject);`**: Uses a custom function (not shown in the provided code) to create a deep copy of `MyObject`. **Pros and Cons** Here's a brief overview of each approach: 1. **Lodash (`_.cloneDeep`)** * Pros: Efficient, widely supported, and easy to use. * Cons: May not be suitable for very large objects due to memory limitations. 2. **JSON parsing (`JSON.parse(JSON.stringify())`)**: * Pros: Simple and widely supported. * Cons: Can lead to issues with circular references, may not work as expected in all browsers. 3. **Custom `deepCopy` function** * Pros: Tailored for specific use cases, efficient, and controlled. * Cons: Requires custom implementation and debugging expertise. 4. **AngularJS (`angular.copy`)** * Pros: Simple to implement, widely supported. * Cons: Only suitable for shallow copies; not ideal for deep copies. 5. **`infoDeepCopyWithNullCheck` function** * Pros: Customizable, efficient (as per implementation). * Cons: Requires custom implementation and debugging expertise. **Other Considerations** When choosing a copy function, consider the following: * **Object size**: For very large objects, more lightweight approaches like Lodash's `cloneDeep` or a custom `deepCopy` function might be necessary to avoid memory issues. * **Browser support**: Ensure the chosen approach is supported by the target browsers. * **Performance**: Compare performance metrics for different approaches to determine the most efficient solution. **Alternatives** Some alternative copy functions you might consider include: * **ES6 spread operator (`{...obj}`)**: Creates a shallow copy of an object, not suitable for deep copies. * **`Object.assign()`**: Creates a shallow copy of an object, not suitable for deep copies. * **`Array.prototype.slice()`**: Creates a shallow copy of an array, not suitable for deep copies. Keep in mind that the choice of copy function ultimately depends on your specific use case and requirements.
Related benchmarks:
Lodash cloneDeep vs JSON Clone vs Deepcopy
Lodash cloneDeep vs JSON Clone vs fastDeepClone
DeepFreeze vs Lodash cloneDeep vs JSON Clone
Object.assign vs. JSON String/Parse vs deepclone
Comments
Confirm delete:
Do you really want to delete benchmark?