Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object cope speed test
(version: 0)
Comparing performance of:
copy recursive vs json vs lodash
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:
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; function copy(o) { var output, v, key; output = Array.isArray(o) ? [] : {}; for (key in o) { v = o[key]; output[key] = (typeof v === "object") ? copy(v) : v; } return output; }
Tests:
copy recursive
myCopy = copy(MyObject);
json
myCopy = JSON.parse(JSON.stringify(MyObject));
lodash
myCopy = _.cloneDeep(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
copy recursive
json
lodash
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):
I'll break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test case that measures the speed of creating a deep copy of an object. The test consists of three individual tests: "copy recursive", "json", and "lodash". Each test uses a different approach to create a copy of the input object, which is defined in the "Script Preparation Code" section. **Benchmark Definition JSON** The benchmark definition contains: * **Name**: A unique name for the benchmark. * **Description**: An optional description of the benchmark. * **Script Preparation Code**: A JavaScript code snippet that defines an object `MyObject` with nested properties, including a deeply nested property `jayson`. This object will be used as input for each test case. * **Html Preparation Code**: A script tag that includes a reference to the Lodash library (version 4.17.5), which is used in one of the test cases. **Individual Test Cases** The three test cases are defined in the "Benchmark Definition" section, which contains: 1. **Copy Recursive**: This test case uses a recursive function `copy` to create a deep copy of the input object. ```javascript function copy(o) { var output, v, key; output = Array.isArray(o) ? [] : {}; for (key in o) { v = o[key]; output[key] = (typeof v === "object") ? copy(v) : v; } return output; } ``` Pros: This approach ensures a complete and accurate deep copy of the input object, as it recursively traverses all nested properties. Cons: Recursive functions can be slower due to function call overhead and stack management. 2. **JSON**: This test case uses the `JSON.parse` method with the `JSON.stringify` method to create a deep copy of the input object. ```javascript myCopy = JSON.parse(JSON.stringify(MyObject)); ``` Pros: This approach is generally faster, as it leverages the optimized `JSON.parse` and `JSON.stringify` methods. Cons: This approach may not work correctly for objects with cyclic references or non- JSON serializable values. 3. **Lodash**: This test case uses the Lodash library's `cloneDeep` function to create a deep copy of the input object. ```javascript myCopy = _.cloneDeep(MyObject); ``` Pros: This approach is often faster and more robust than using built-in methods, as it handles complex data structures and edge cases. Cons: This approach requires including an external library (Lodash), which may add overhead. **Test Results** The latest benchmark results show the performance of each test case on a Chrome 85 browser running on a Windows Desktop. The results are: 1. **Copy Recursive**: Executions per second = 943,678.25 2. **Lodash**: Executions per second = 508,200.21875 3. **JSON**: Executions per second = 275,258.125 The results indicate that the "copy recursive" approach is significantly faster than the other two approaches. **Other Alternatives** For creating deep copies of objects in JavaScript, other alternatives include: 1. **Array methods**: Using `Array.prototype.slice()` or `Array.prototype.map()` to create a shallow copy of an array or object. 2. **Object.assign()**: Using `Object.assign()` to create a shallow copy of an object. 3. **For...in loops**: Using a For...in loop with a temporary variable to iterate over the object's properties and assign values. However, these alternatives are not suitable for creating deep copies of complex objects with nested properties, as they may not handle recursive or cyclic references correctly.
Related benchmarks:
Lodash cloneDeep vs JSON Clone (very big object)
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. fastest-json-copy | On a Small Object
Lodash cloneDeep vs JSON Clone vs Object Spread
lodash clonedeep vs json.parse(stringify()) vs recursivecopy heavy
Comments
Confirm delete:
Do you really want to delete benchmark?