Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
testingCloneObject2
(version: 0)
Comparing performance of:
vanila vs JSON.parse vs lodash
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
Script Preparation code:
var bob = { name: "Bob", age: 32, lastName: "test", local: {there:"there"}, test:{ test:{ test:"test" } } };
Tests:
vanila
function cloneObject(obj) { if (obj === null || typeof obj !== 'object') { return obj; } var temp = obj.constructor(); // give temp the original obj's constructor for (var key in obj) { temp[key] = cloneObject(obj[key]); } return temp; } var bill = (cloneObject(bob));
JSON.parse
var bill2 = (JSON.parse(JSON.stringify(bob)));
lodash
var deep = _.cloneDeep(bob);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
vanila
JSON.parse
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):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark definition is provided in JSON format, which outlines two scripts: `Script Preparation Code` and `Html Preparation Code`. The `Script Preparation Code` defines an object `bob` with various properties, including nested objects. The `Html Preparation Code` includes a reference to the Lodash library. **Options Compared** The benchmark compares three different approaches for cloning the `bob` object: 1. **Vanilla JavaScript**: This approach uses a custom function `cloneObject` that recursively clones the object using its constructor and properties. 2. **JSON.parse**: This approach uses the `JSON.parse` method to clone the object by stringifying it, parsing it back as JSON, and then converting the resulting JSON string back to an object. 3. **Lodash**: This approach uses the Lodash library's `cloneDeep` function to deep-clone the object. **Pros and Cons of Each Approach** 1. **Vanilla JavaScript**: * Pros: Easy to implement, no external dependencies required. * Cons: May be slower due to its iterative nature and potential performance overhead. 2. **JSON.parse**: * Pros: Simple and fast, as it leverages the built-in `JSON` object. * Cons: May not work correctly for nested objects with circular references or non-serializable properties. 3. **Lodash**: * Pros: Provides a robust and efficient way to clone objects, especially for deep cloning. * Cons: Requires an external dependency (Lodash library). **Library and Its Purpose** The Lodash library is used in the benchmark to provide a convenient and efficient way to perform deep cloning of complex objects. **Special JS Feature or Syntax** None mentioned in this specific benchmark. **Other Considerations** When testing benchmarking results, it's essential to consider factors such as: * **Platform and browser**: The benchmark was run on Chrome 53 on a Mac with OS X 10.11.6. * **Device platform**: This may affect the performance of the browser or rendering engine. * **Number of executions per second**: A higher value indicates faster performance. **Alternatives** Other approaches for cloning objects in JavaScript include: * Using the `Object.assign` method with a deep copy function (e.g., `deepcopy` from the `lodash` library). * Utilizing libraries like `json-stringify-safe` or `json-deep-clone`. * Implementing custom recursive functions similar to the vanilla JavaScript approach. Keep in mind that the performance difference between these approaches may be negligible for simple cloning tasks, but it's essential to consider factors like memory usage and overhead when choosing a method.
Related benchmarks:
Object.assign vs Lodash.assign
circleTest
Checks if value is an object
object iteration pravin
aadasdsa
Comments
Confirm delete:
Do you really want to delete benchmark?