<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 };
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = Object.assign({}, a, b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash merge | |
object.assign | |
spread | |
assign (no overwrite) |
Test name | Executions per second |
---|---|
lodash merge | 3466734.0 Ops/sec |
object.assign | 6491242.0 Ops/sec |
spread | 2533773.5 Ops/sec |
assign (no overwrite) | 5100183.0 Ops/sec |
Overview
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The goal of this benchmark is to compare the performance of different methods for merging two objects in JavaScript.
Method Comparison
Four methods are compared:
merge
: This method uses the Lodash library to merge two objects. It returns a new object with merged properties from both input objects.Object.assign
: This method is used to assign values from one or more source objects to a target object. It returns the target object with assigned properties....
): This operator creates a new object by spreading properties from an existing object(s) into a new object. In this test, it's used to merge two objects.Object.assign
(no overwrite): Similar to the previous method but with an additional check to prevent overwriting properties in the target object.Pros and Cons
merge
: Pros:Object.assign
: Pros:...
): Pros:Object.assign (no overwrite)
: Pros:Cons:
merge
:Object.assign
:...
):Library Usage
The lodash.merge
method is the only one that uses an external library (Lodash). This adds a small overhead due to library initialization and parsing. However, it simplifies the code and makes object merging more concise.
JavaScript Features
No special JavaScript features are used in this benchmark test case.
Alternative Approaches
Some alternative approaches for merging objects include:
concat
method with arraysObject.create()
or a custom class)JSON.parse(JSON.stringify(obj1, obj2))
deepmerge
or merge-deep
Keep in mind that these alternatives may have different performance characteristics and might not be as straightforward to use.
Performance Considerations
In terms of performance, the benchmark results show that:
merge
: The slowest method, likely due to the external library overhead.Object.assign (no overwrite)
: Faster than Object.assign
, possibly because it avoids the native object lookup and copying process for property keys....
): Similar performance to Object.assign
, with a slight advantage due to its conciseness and simplicity.Conclusion
The choice of method ultimately depends on your specific use case, personal preference, or project requirements. The MeasureThat.net benchmark provides an excellent starting point for exploring object merging in JavaScript.