Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RecursiveDeepCopyTTTeam
(version: 0)
Comparing performance of:
Loadash vs JSON Parse vs recursiveDeepCopy vs recursiveDeepCopyTTTeam
Created:
2 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 recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; } function recursiveDeepCopyTTTeam(o) { let newO; let i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if (Object.prototype.toString.apply(o) === '[object Array]') { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; Object.keys(o).forEach((key) => { newO[key] = recursiveDeepCopy(o[key]); }); return newO; }
Tests:
Loadash
myCopy = _.cloneDeep(MyObject);
JSON Parse
myCopy = JSON.parse(JSON.stringify(MyObject));
recursiveDeepCopy
myCopy = recursiveDeepCopy(MyObject);
recursiveDeepCopyTTTeam
myCopy = recursiveDeepCopyTTTeam(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Loadash
JSON Parse
recursiveDeepCopy
recursiveDeepCopyTTTeam
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Loadash
771005.4 Ops/sec
JSON Parse
629910.2 Ops/sec
recursiveDeepCopy
4512344.5 Ops/sec
recursiveDeepCopyTTTeam
2783028.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition JSON** The provided JSON represents a benchmark definition, which includes: 1. **Script Preparation Code**: This section contains the code that is used to prepare the benchmark script for execution. In this case, it defines an object `MyObject` with various properties and methods. 2. **Html Preparation Code**: This section specifies the HTML code required to load the necessary libraries or scripts before running the benchmark. **Individual Test Cases** Each test case represents a separate benchmark that tests different approaches for creating a deep copy of the `MyObject` object: 1. **Loadash**: This test case uses the `lodash.cloneDeep` function from the Lodash library to create a deep copy of the `MyObject`. 2. **JSON Parse**: This test case attempts to parse the JSON representation of the `MyObject` using the `JSON.parse()` method. 3. **recursiveDeepCopy**: This test case implements its own recursive function, `recursiveDeepCopy`, to create a deep copy of the `MyObject`. 4. **recursiveDeepCopyTTTeam**: This test case is similar to the previous one but uses an alternative implementation with some minor differences in variable names and syntax. **Comparison of Approaches** Let's compare the four approaches: 1. **Lodash (Loadash)**: This approach uses a popular library to create a deep copy of the object, which is likely to be fast and efficient. * Pros: Fast, reliable, and widely used. * Cons: Requires an external library, which might not be desirable for all scenarios. 2. **JSON Parse**: This approach attempts to parse the JSON representation of the object, which may not work as expected if the object has complex nested structures. * Pros: Simple and doesn't require any external libraries. * Cons: May not work correctly for all objects or scenarios. 3. **recursiveDeepCopy**: This approach implements a custom recursive function to create a deep copy of the object. While it's possible that this implementation is optimized for performance, it's still a custom solution. * Pros: Customizable and might be optimized for specific use cases. * Cons: Requires manual implementation, which can lead to errors or inefficiencies. 4. **recursiveDeepCopyTTTeam**: This approach is similar to the previous one but with some minor differences in variable names and syntax. * Pros: Similar benefits as recursiveDeepCopy. * Cons: Same drawbacks as recursiveDeepCopy. **Other Considerations** When choosing an approach, consider factors such as: 1. Performance: How fast does each approach execute? 2. Reliability: Are the approaches reliable for all possible input scenarios? 3. Maintenance: How easy is it to maintain and update each implementation? 4. Complexity: How complex are the implementations compared to their performance? **Alternatives** If you're looking for alternative approaches, consider: 1. Using a more advanced library like `JSON5` or `json-stringify-safe` for JSON serialization/deserialization. 2. Exploring other deep copy libraries like `Immer` or `ramda`. 3. Implementing a custom solution using a different programming paradigm (e.g., functional programming). Keep in mind that each approach has its strengths and weaknesses, and the best choice depends on your specific use case and requirements.
Related benchmarks:
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone 2
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new
lodash clonedeep vs json.stringify()
Comments
Confirm delete:
Do you really want to delete benchmark?