<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = _.assign(a, b);
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = Object.assign(a, b);
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = { a, b };
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash assign | |
object.assign | |
spread |
Test name | Executions per second |
---|---|
lodash assign | 2518996.5 Ops/sec |
object.assign | 3177520.5 Ops/sec |
spread | 1555037.6 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and considered.
What is being tested?
The benchmark measures the performance difference between three approaches for merging objects:
lodash.assign
object.assign
(built-in JavaScript method){ ...a, ...b }
)These approaches are being compared to merge two objects: a
and b
. The resulting object is stored in variable c
.
Options comparison
The three options are:
_assign
function: A popular utility library for functional programming tasks.object.assign
: A built-in method for merging objects, introduced in ECMAScript 2015 (ES6).{ ...a, ...b }
): A shorthand way of creating a new object by spreading the properties of two existing objects.Pros and Cons of each approach
_assign
function:object.assign
:{ ...a, ...b }
):Other considerations
object.assign
is the fastest approach, followed by spread syntax, and then Lodash's _assign
function._assign
function may add some overhead due to its own implementation, but it provides more control over merge operations and can be useful in other scenarios.Library explanations
The benchmark uses Lodash version 4.17.5, which is a popular utility library for functional programming tasks.
Special JS feature or syntax explanation
There are no special features or syntaxes being tested in this benchmark. The focus is on comparing three established approaches for merging objects.
In summary, the benchmark aims to determine which approach (Lodash's _assign
function, object.assign
, or spread syntax) is the fastest and most efficient way to merge two objects.