<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var c = _.merge({ a: 'oh', b: 'my' }, { c: 'goddess' });
var c = Object.assign({ a: 'oh', b: 'my' }, { c: 'goddess' });
var c = { { a: 'oh', b: 'my' }, c: 'goddess' };
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash merge | |
object.assign | |
spread |
Test name | Executions per second |
---|---|
lodash merge | 2647033.8 Ops/sec |
object.assign | 5045269.5 Ops/sec |
spread | 1758505.4 Ops/sec |
What is being tested?
The provided JSON represents a benchmark test that compares the performance of three different approaches to merge two objects in JavaScript: lodash.merge
, Object.assign
, and the spread operator (...
). The test case uses no intermediate variables.
Options comparison
Here's a brief overview of each approach, their pros and cons:
lodash.merge
takes two objects as input and returns a new object containing the merged properties....
): Introduced in ECMAScript 2015, the spread operator allows you to expand an array-like object into individual arguments.Library usage
The test uses the lodash
library for the lodash.merge
approach. Lodash is a popular utility library that provides various functions for tasks like array manipulation, object merging, and more. It's widely used in JavaScript applications due to its extensive feature set and efficient implementation.
Special JS features or syntax
There are no special JavaScript features or syntaxes used in this benchmark test. All three approaches use standard JavaScript constructs.
Other alternatives
If lodash
is not an option, other alternatives for merging objects include:
Object.assign()
method with a third argument to specify the target object.for...in
, Object.keys()
, and other built-in methods.underscore.js
or ramda
.However, it's worth noting that these alternatives might not be as efficient or concise as lodash.merge
or the spread operator.
Benchmark interpretation
The benchmark results show the execution time (in executions per second) for each test case on a Chrome 86 browser running on Mac OS X 10.15.5. The top performer is Object.assign
, followed by the spread operator, and then lodash.merge
.