base = {
asdsA: 'dasdsa',
dsad: 434534,
hji: 'asdsa',
};
second = {
thing: 'hey'
};
createCopy = (base, other) => {
return {
asdsA: base.asdsA,
dsad: base.dsad,
hji: base.hji,
thing: other.thing,
};
};
Object.assign({}, base)
Object.assign({}, base, second);
createCopy(base, second);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple Assign | |
Assign joining 2 objects | |
Manually joining 2 objects |
Test name | Executions per second |
---|---|
Simple Assign | 2992828.0 Ops/sec |
Assign joining 2 objects | 2053949.2 Ops/sec |
Manually joining 2 objects | 2519832.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is testing three different approaches to join two objects: Object.assign
with an empty object (Simple Assign
), Object.assign
with another object (Assign joining 2 objects
), and a custom function (createCopy
) that manually joins the two objects.
Options being compared
base
object and then assigns the second object (second
) to it.base
and second
objects to a new object.createCopy
) that takes two objects as input, creates a new object with properties from the first object, and then adds properties from the second object.Pros and Cons
base
and only assigns properties from second
.base
and second
are joined into a single object.Libraries and special JS features
None of the options rely on external libraries. However, note that Object.assign
is a built-in JavaScript method.
No special JS features are required for this benchmark.
Other alternatives
If you wanted to test other approaches, you could consider:
JSON.parse(JSON.stringify(base))
with second
instead of Object.assign
.const
vs. var
or let
for variable declarations.ArrayBuffer
, TypedArray
) when joining objects.Keep in mind that these alternatives would require significant changes to the benchmark definition and test cases, but could provide additional insights into JavaScript performance.