Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs object.assing vs forEach
(version: 0)
Comparing performance of:
Spread vs Object.assign vs Foreach
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
const a = { email: "abc@mail.com", phone: "12345678", }; const b = { app_version: "AP.12", appsflyer_id: "askldjajkshdlkjh2323h4234j2l3j4h", advertising_id: "23;o4h2l34hl2kj3h4kj23h4", device_os_version: "12", device_name: "Aifon 15 PRO", firebase_instance_id: "2liu3hiuo23y4u23y4ui2y34iy3u4y234", }; const res = { email: null, phone: null, last_name: null, first_name: null, app_version: null, appsflyer_id: null, advertising_id: null, device_os_version: null, device_name: null, firebase_instance_id: null, ...a, ...b };
Object.assign
const a = { email: "abc@mail.com", phone: "12345678", }; const b = { app_version: "AP.12", appsflyer_id: "askldjajkshdlkjh2323h4234j2l3j4h", advertising_id: "23;o4h2l34hl2kj3h4kj23h4", device_os_version: "12", device_name: "Aifon 15 PRO", firebase_instance_id: "2liu3hiuo23y4u23y4ui2y34iy3u4y234", }; const res = Object.assign({ email: null, phone: null, last_name: null, first_name: null, app_version: null, appsflyer_id: null, advertising_id: null, device_os_version: null, device_name: null, firebase_instance_id: null }, a, b);
Foreach
const a = { email: "abc@mail.com", phone: "12345678", last_name: "Last Name", first_name: "First Name", app_version: null, appsflyer_id: null, advertising_id: null, device_os_version: null, device_name: null, firebase_instance_id: null }; const b = { app_version: "AP.12", appsflyer_id: "askldjajkshdlkjh2323h4234j2l3j4h", advertising_id: "23;o4h2l34hl2kj3h4kj23h4", device_os_version: "12", device_name: "Aifon 15 PRO", firebase_instance_id: "2liu3hiuo23y4u23y4ui2y34iy3u4y234", }; const res = { email: null, phone: null, last_name: null, first_name: null, app_version: null, appsflyer_id: null, advertising_id: null, device_os_version: null, device_name: null, firebase_instance_id: null }; Object.keys(a).forEach(key => { res[key] = a[key]; }); Object.keys(b).forEach(key => { res[key] = b[key]; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Spread
Object.assign
Foreach
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):
Measuring the performance of different JavaScript methods for object creation and assignment is crucial in understanding the efficiency of code execution. **Benchmark Overview** The benchmark compares three methods: `Spread`, `Object.assign`, and `Foreach`. The test case involves creating two objects, `a` and `b`, with varying properties. Each method is used to assign these objects to a new object `res`. **Method Comparison** ### Spread * **Description**: The spread operator (`...`) is used to expand an object's properties into a new object. * **Pros**: Fast and concise way to create a new object by copying existing ones. It also works well with arrays and other iterable objects. * **Cons**: Limited control over the resulting object, as it only copies enumerable properties. ### Object.assign() * **Description**: `Object.assign()` is used to copy properties from one or more source objects to a target object. * **Pros**: Allows for more control over the resulting object by specifying which properties to copy. It also works with objects and arrays. * **Cons**: Can be slower than spread operator due to its function call overhead, especially when dealing with large objects. ### Foreach * **Description**: The `forEach()` method is used on arrays or iterable objects to execute a callback function for each element. * **Pros**: Allows for control over the resulting object by specifying which properties to copy. It also works with objects and arrays. * **Cons**: Slower than spread operator due to its function call overhead, especially when dealing with large objects. **Other Considerations** When choosing between these methods, consider the following factors: * Performance: Spread operator is generally faster than `Object.assign()` or `forEach()`. * Control: `Object.assign()` and `Foreach()` offer more control over the resulting object compared to the spread operator. * Code Readability: The choice of method depends on personal preference, but generally, spread operator is considered more concise. **Browser Variations** The provided benchmark results are for Chrome 114 on a Mac OS X system. Different browsers may exhibit different performance variations due to their internal optimizations and caching mechanisms.
Related benchmarks:
JavaScript spread operator vs Object.assign performance to merge into new object
JavaScript spread operator vs Object.assign performance (with empty object in Object.assign)
JavaScript spread operator vs Object.assign vs for-in loop performance
JavaScript spread operator vs Object.assign vs for-in loop safe performance
JavaScript spread operator vs Object.assign performance with {} target obj
Comments
Confirm delete:
Do you really want to delete benchmark?