Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Merge objects performance test
(version: 0)
Comparing performance of:
Spread vs Object.assign vs Foreach vs just for
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
let res = { 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 c = { first_name: "lalala", last_name: "dadada" }; 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, ...res, ...b }; 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, ...res, ...c }
Object.assign
let res = { 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 c = { first_name: "lalala", last_name: "dadada" }; 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, ...res }; Object.assign(res, b); Object.assign(res, c);
Foreach
let res = { 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 c = { first_name: "lalala", last_name: "dadada" }; 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, ...res }; Object.keys(b).forEach(key => { res[key] = b[key]; }); Object.keys(c).forEach(key => { res[key] = c[key]; });
just for
let res = { 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 c = { first_name: "lalala", last_name: "dadada" }; 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, ...res }; for (const key in b) { res[key] = b[key]; } for (const key in c) { res[key] = c[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Spread
Object.assign
Foreach
just for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
5163386.5 Ops/sec
Object.assign
11650718.0 Ops/sec
Foreach
3487604.2 Ops/sec
just for
5712879.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Based on the provided benchmark results, I'll provide an analysis of the performance differences between three different methods: `Object.assign()`, `for...in` loop with `forEach()` (Foreach), and simple assignment. **Method 1: Object.assign()** * **Test Name:** "Object.assign" * **Executions Per Second:** 4619255.0 * **Device Platform:** Desktop The `Object.assign()` method is a modern JavaScript feature that allows assigning properties from one or more source objects to a target object. This approach is fast and efficient, requiring minimal overhead. **Method 2: For...in loop with forEach() (Foreach)** * **Test Name:** "Foreach" * **Executions Per Second:** 3510965.0 * **Device Platform:** Desktop The `for...in` loop with `forEach()` is a traditional JavaScript approach to iterating over objects. While it may seem slower compared to `Object.assign()`, the execution speed is still respectable, especially considering the explicit iteration. **Method 3: Simple assignment** * **Test Name:** "just for" * **Executions Per Second:** 6470421.5 * **Device Platform:** Desktop This method involves simple assignment of properties from two source objects (`b` and `c`) to a target object (`res`). While it might seem straightforward, this approach still exhibits decent performance. **Key observations:** 1. The order of the methods in terms of execution speed is `Object.assign()` > Simple Assignment > `For...in` loop with `forEach()`. 2. Both `Object.assign()` and simple assignment are significantly faster than using a traditional `for...in` loop, which might be attributed to the inherent efficiency of modern JavaScript object manipulation. 3. The performance difference between these methods is relatively small, especially considering the device platform (Desktop) and execution speed. **Conclusion:** In conclusion, based on the provided benchmark results, it appears that `Object.assign()` remains the fastest method for assigning properties from multiple source objects to a target object. While simple assignment and traditional loops (`for...in` with `forEach()`) are still respectable in terms of performance, they don't quite match the efficiency of `Object.assign()`.
Related benchmarks:
Merge objects comparison
JavaScript spread operator vs Object.assign performance vs a custom merge object method
compare _.merge() and lodash-merge
Merge objects: Lodash vs Native112312321321xcx
merge test - lodash vs native
Comments
Confirm delete:
Do you really want to delete benchmark?