Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs object.assing vs forEach 2
(version: 0)
Comparing performance of:
Spread vs Object.assign vs Foreach
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
for (let i=0; i<1000; i++) { const a = { email: "abc@mail.com" + i, phone: "12345678" + i, 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" + i, appsflyer_id: "askldjajkshdlkjh2323h4234j2l3j4h" + i, advertising_id: "23;o4h2l34hl2kj3h4kj23h4" + i, device_os_version: "12", device_name: "Aifon 15 PRO", firebase_instance_id: "2liu3hiuo23y4u23y4ui2y34iy3u4y234" + i, }; 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
for (let i=0; i<1000; i++) { const a = { email: "abc@mail.com" + i, phone: "12345678" + i, 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" + i, appsflyer_id: "askldjajkshdlkjh2323h4234j2l3j4h" + i, advertising_id: "23;o4h2l34hl2kj3h4kj23h4" + i, device_os_version: "12", device_name: "Aifon 15 PRO", firebase_instance_id: "2liu3hiuo23y4u23y4ui2y34iy3u4y234" + i, }; 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
for (let i=0; i<1000; i++) { const a = { email: "abc@mail.com" + i, phone: "12345678" + i, 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" + i, appsflyer_id: "askldjajkshdlkjh2323h4234j2l3j4h" + i, advertising_id: "23;o4h2l34hl2kj3h4kj23h4" + i, device_os_version: "12", device_name: "Aifon 15 PRO", firebase_instance_id: "2liu3hiuo23y4u23y4ui2y34iy3u4y234" + i, }; 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):
Let's dive into the explanation. The benchmark is designed to compare three different ways of creating an object with similar properties: using the spread operator (`Spread`), `Object.assign()` (with two objects as arguments, i.e., `Object.assign({}, obj1, obj2)`), and a manual loop with `forEach` (`Foreach`). **Options Compared:** * **Spread**: Using the spread operator to create a new object by spreading two existing objects. * **Object.assign()**: Using the `Object.assign()` method to merge two or more objects into one. * **Foreach**: Manual iteration using `forEach` to create an object with similar properties. **Pros and Cons:** ### Spread Pros: * Concise and readable syntax * Creates a new object without modifying the original objects * Fast and efficient, especially for small objects Cons: * Requires JavaScript version 3.0 or higher (not supported in older browsers) * Can lead to slower performance if used extensively due to potential string concatenation issues ### Object.assign() Pros: * Wide browser support (including older versions) * Can handle merging objects with different property types * Fast and efficient for large objects Cons: * Requires two objects as arguments, which can be less readable than the spread operator syntax * May lead to slower performance if used extensively due to potential object creation overhead ### Foreach Pros: * No limitations on JavaScript version or browser support * Can handle merging objects with different property types * Manual control over object creation and iteration Cons: * Requires more code and manual iteration, which can be less readable and error-prone * May lead to slower performance due to the overhead of iterating through properties **Benchmark Results:** The latest benchmark results show that the order of performance is: 1. `Foreach` (average 784 executions per second) 2. `Object.assign()` (average 1486 executions per second) 3. `Spread` (average 1736 executions per second) However, it's essential to note that these results may vary depending on the specific use case, object size, and browser performance. In general, the spread operator is a good choice when you need to create a new object with similar properties in modern browsers. `Object.assign()` is still a viable option for older browsers or large objects, while manual iteration using `forEach` provides complete control over object creation and iteration.
Related benchmarks:
JavaScript spread operator vs Object.assign performance to merge into new object
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
JavaScript spread operator vs Object.assign performance GifCo
Comments
Confirm delete:
Do you really want to delete benchmark?