var voucher = {
"single": false,
"applyOverTaxes": false,
"discountType": "1",
"value": 100,
"minimumAmount": 0,
"quantity": 1,
"suffixLength": 8,
"code": "IF5R0AFX",
"vertical": [],
"allowedPaymentMethods": [],
"allowedBanks": [],
"types": [],
"partners": [],
"startDate": "2020-10-21T00:00:00.000Z",
"endDate": "2020-10-21T00:00:00.000Z",
"applyed": false
}
var voucherQuantityAndPriceUpdated = {
value: 120,
quantity: 10
}
const keysOfObjectWithUpdatedData = Object.keys(voucherQuantityAndPriceUpdated)
for (key of keysOfObjectWithUpdatedData) {
const updatedPropertyValue =
voucher[key] = voucherQuantityAndPriceUpdated[key]
}
const voucherUpdated = {
voucher,
voucherQuantityAndPriceUpdated
}
voucher.value = voucherQuantityAndPriceUpdated.value
voucher.quantity = voucherQuantityAndPriceUpdated.quantity
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using Object keys and modifying the original object | |
Using spread to merge objects | |
Manually editing every single property (Wanna have this to prove why sometimes there is a manual assign for each property required) |
Test name | Executions per second |
---|---|
Using Object keys and modifying the original object | 3548374.0 Ops/sec |
Using spread to merge objects | 734079.1 Ops/sec |
Manually editing every single property (Wanna have this to prove why sometimes there is a manual assign for each property required) | 155488528.0 Ops/sec |
Let's break down the provided benchmark.
Benchmark Description
The benchmark is designed to compare three approaches for partially assigning properties from one object to another:
Object.keys()
to get keys of an object and then iterating over those keys to assign values....
) to merge two objects.What is being tested?
The benchmark tests which approach is faster, assuming that the size of the voucher
and voucherQuantityAndPriceUpdated
objects are fixed.
Options Compared
Object.keys()
: This method iterates over the keys of the voucher
object using a for...of
loop and then assigns values from voucherQuantityAndPriceUpdated
to each key in the original object....
): This method creates a new object that is a copy of voucher
, and then merges voucherQuantityAndPriceUpdated
into this new object using the spread operator.voucherQuantityAndPriceUpdated
to corresponding keys in voucher
.Pros and Cons
Object.keys()
:...
):voucher
.voucher
has complex properties or if the order of properties matters.Library/Functionality
None mentioned in this benchmark definition. The benchmark focuses on comparing different approaches rather than utilizing external libraries or functions.
Special JS Feature/Syntax
No special JavaScript features or syntax are used in this benchmark. All code is standard JavaScript.
Other Considerations
voucher
and voucherQuantityAndPriceUpdated
objects is fixed, which may not be the case in all scenarios.Alternatives
Object.assign()
, creating a new object with Object.create()
and then assigning values, or using a library like Lodash.