<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 = _merge(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 merge | |
spread operator |
Test name | Executions per second |
---|---|
lodash merge | 0.0 Ops/sec |
spread operator | 6133651.0 Ops/sec |
Let's break down the provided JSON and benchmark results.
Benchmark Definition:
The benchmark is called "Array.prototype.concat vs spread operator". The description indicates that this test compares two methods of merging arrays in JavaScript:
concat()
method...
)Test Cases:
There are two individual test cases:
This test case uses the _merge
function from the Lodash library to merge two objects, a
and b
.
merge
.merge
function recursively merges values in two objects.The test case code is:
var a = { a: 'oh', b: 'my' }
var b = { c: 'goddess' }
var c = _merge(a, b)
This test case uses the spread operator (...
) to merge two objects, a
and b
.
...
)The test case code is:
var a = { a: 'oh', b: 'my' }
var b = { c: 'goddess' }
var c = { a, ...b }
Benchmark Results:
The latest benchmark result shows two tests:
0
executions per second).Comparison:
The spread operator approach appears to be significantly faster than using the Lodash _merge
function.
Pros and Cons:
Alternatives:
Other alternatives for merging objects include:
Object.assign()
method (not tested in this benchmark)Keep in mind that these results are specific to Chrome 65 and might vary on other browsers or environments.