<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 = 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 merge | |
object.assign | |
spread |
Test name | Executions per second |
---|---|
lodash merge | 1743213.4 Ops/sec |
object.assign | 4312633.5 Ops/sec |
spread | 2619901.0 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Overview
The benchmark compares the performance of three approaches for merging objects: lodash.merge
, object.assign
, and spread operator ({ ... }
). The test cases are identical, with two source objects (a
and b
) merged into a new object (c
).
Options Compared
_.merge()
function from Lodash library to merge the objects.Object.assign()
method to merge the objects.{ ... }
) to merge the objects.Pros and Cons of Each Approach
Library Used
In this benchmark, Lodash is used as a library for its merge()
function. The library provides a simple and efficient way to merge objects.
Special JS Feature/Syntax
The spread operator ({ ... }
) is a relatively recent addition to JavaScript, introduced in ECMAScript 2018 (ES10). It allows for more concise object creation and merging.
Other Considerations
JSON.parse()
and JSON.stringify()
, could also be used for object merging. However, these methods are generally slower and less efficient than the three approaches compared in this benchmark.In summary, the benchmark compares the performance of three object merging approaches: Lodash Merge, Object Assign, and Spread Operator. Each approach has its pros and cons, and understanding these trade-offs can help developers choose the most suitable method for their specific use case.