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 c = {
first_name: "lalala",
last_name: "dadada"
};
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,
c
};
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 c = {
first_name: "lalala",
last_name: "dadada"
};
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, c);
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 c = {
first_name: "lalala",
last_name: "dadada"
};
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
};
Object.keys(b).forEach(key => {
res[key] = b[key];
});
Object.keys(c).forEach(key => {
res[key] = c[key];
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Object.assign | |
Foreach |
Test name | Executions per second |
---|---|
Spread | 8618732.0 Ops/sec |
Object.assign | 6108910.0 Ops/sec |
Foreach | 3536326.2 Ops/sec |
Measuring the performance of different JavaScript methods for iterating over objects is an essential task, especially when working with large datasets.
Benchmark Test
The benchmark test measures the time taken to create an object res
by spreading one or more objects (a
, b
, or c
) onto another object, using either the spread operator (...
), Object.assign()
, or a loop (forEach()
).
Options Compared
There are three options being compared:
...
): This method creates a new object by spreading the properties of one or more objects onto another object.Object.assign()
: This method creates a new object by copying the values from multiple source objects to a target object.forEach()
Loop: This method uses a loop to iterate over the keys of an object and assigns the corresponding values from one or more objects to a target object.Pros and Cons
...
)Object.assign()
forEach()
LoopObject.assign()
due to the overhead of iterating over the keys and assigning values manually.Library Used
None. The benchmark test uses built-in JavaScript methods (...
, Object.assign()
, and forEach()
).
Special JS Feature/Syntax
The spread operator (...
) is a new feature introduced in ECMAScript 2018 (ES9). It allows creating a new object by spreading the properties of one or more objects onto another object.
Benchmark Results
According to the latest benchmark results, the order of performance from fastest to slowest is:
forEach()
LoopObject.assign()
...
)Note that these results may vary depending on the specific use case and hardware configuration.