Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setting property vs Object.assign
(version: 0)
Comparing performance of:
Object.assign property vs set property
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var user = { id: 1, email: 'bob@somewhere.com', personalInfo: { name: 'Robert', address: { road: 'Quartier Djinageryber', city: 'Timbuktu', country: 'Mali' } } } function deepClone (source) { if (source instanceof Date) return new Date(source.getTime()) if (Array.isArray(source)) { const clone = [] let i = source.length while (i--) { const value = source[i] clone[i] = (value !== null && typeof value === 'object') ? deepClone(value) : value } return clone } const clone = {} for (const key in source) { if ({}.hasOwnProperty.call(source, key)) { const value = source[key] clone[key] = (value !== null && typeof value === 'object') ? deepClone(value) : value } } return clone } function assignField (key, value, obj) { return Object.assign(deepClone(obj), { [key]: value }) } function setField (key, value, obj) { const clone = deepClone(obj) clone[key] = value return clone }
Tests:
Object.assign property
assignField('email', 'robert@somewhere.com', user)
set property
setField('email', 'robert@somewhere.com', user)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign property
set property
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):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark that compares the performance of two approaches: using `Object.assign` to set properties and using a custom function `setField`. The benchmark is designed to test the speed of these two approaches in setting properties on an object. **Tested Options** There are only two options being compared: 1. **Using `Object.assign`**: This approach uses the built-in `Object.assign()` method to assign values to the `user` object. 2. **Custom `setField` function**: This is a custom function written by the benchmark author that creates a deep clone of the original object using another custom function called `deepClone`, and then assigns the new value using `Object.assign`. **Pros and Cons** * **Using `Object.assign`**: + Pros: Fast, widely supported, and efficient. + Cons: Can be slower than custom implementations for large objects or complex assignments. * **Custom `setField` function**: + Pros: Allows for more control over the cloning process, which can be beneficial for complex objects. Also, can potentially outperform `Object.assign()` for very large assignments due to reduced overhead. + Cons: Requires additional code and potential performance overhead due to the creation of a deep clone. **Deep Clone Function** The custom `deepClone` function is used to create a deep copy of the original object. It recursively clones all nested objects, arrays, and primitive values, making it suitable for handling complex data structures. * **Pros**: Provides a reliable way to handle cloning of complex objects. * **Cons**: Introduces additional overhead due to the creation of an intermediate clone. **Comparison** The benchmark is likely comparing these two approaches in terms of performance, speed, or both. The results will help determine which approach is more efficient for setting properties on objects. **Other Alternatives** Some other alternatives that might be considered when setting properties on objects include: * Using the bracket notation (`user['email'] = 'robert@somewhere.com';`) * Using a library like Lodash (which provides a `set` function) * Using a framework's built-in property setter functions (e.g., React's `useState` hook) However, these alternatives might not be directly comparable to the custom `setField` function or `Object.assign`, as they might have different use cases, performance characteristics, or trade-offs.
Related benchmarks:
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
test for Vovan
Cloning b81d8fae-190a-4c8b-a9af-08bebc52bf2a
Object.assign vs. JSON String/Parse vs deepclone
Comments
Confirm delete:
Do you really want to delete benchmark?