<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
function jsMerge(target, sources) {
if (!sources.length) {
return target;
}
sources.forEach((src) => {
Object.keys(src).forEach((key) => {
const value = src[key];
if (Object.prototype.hasOwnProperty.call(target, key)) {
// target already has key. Merge if required.
if (typeof target[key] === 'object' && typeof value === 'object') {
// Try to merge objects or arrays
if (Array.isArray(value)) {
if (Array.isArray(target[key]) && target[key].length > value.length) {
// Both arrays and old array is longer. Merge
target[key] = Array.prototype.concat(value, Array.prototype.slice.call(target[key], value.length));
} else {
// Either not both arrays or old array is same length/shorter. Overwrite.
target[key] = value;
}
} else {
// Both non-array objects. Recursively merge
target[key] = jsMerge(target[key], value);
}
} else {
// At least one side is not object/array. Overwrite.
target[key] = value;
}
} else {
// Key does not exist on object. Assign.
target[key] = value;
}
});
});
return target;
}
var a1 = { a: 'oh', b: 'my', c: 'goodness', d: { a: 'nested', b: 'object', c: [ 'arrayA', 'arrayB', 'arrayC' ], e: 'a.d.e' } };
var b1 = { c: 'GOODNESS', d: { b: 'object!', c: [ 'array1', 'array2' ], d: 'b.d.d' } };
jsMerge(a1, b1);
var a2 = { a: 'oh', b: 'my', c: 'goodness', d: { a: 'nested', b: 'object', c: [ 'arrayA', 'arrayB', 'arrayC' ], e: 'a.d.e' } };
var b2 = { c: 'GOODNESS', d: { b: 'object!', c: [ 'array1', 'array2' ], d: 'b.d.d' } };
_.merge(a2, b2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
recursive indexed setter merge function | |
lodash merge |
Test name | Executions per second |
---|---|
recursive indexed setter merge function | 1089465.5 Ops/sec |
lodash merge | 319668.3 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Definition
The benchmark tests two different approaches for merging objects:
jsMerge
. It recursively merges the properties of two objects based on their types.merge
function, which also recursively merges objects.Options Compared
The main difference between these two approaches is how they handle arrays and nested objects:
Array.prototype.concat
and Array.prototype.slice
.Pros and Cons
Other Considerations
When choosing between these two approaches, consider the trade-offs between performance, readability, and maintainability. If you need more control over the merge process or are working with complex data structures, jsMerge
might be a better choice. However, if you prioritize speed and ease of maintenance, Lodash's merge
function is likely a better option.
Library Used
The benchmark uses Lodash version 4.17.21 in its Html Preparation Code
. Lodash is a popular JavaScript library that provides a set of utility functions for tasks like array manipulation, object creation, and more.
Special JS Feature or Syntax
There are no special JavaScript features or syntax used in this benchmark. The code uses standard ES6 syntax and only relies on built-in JavaScript functionality.
Now that we've broken down the benchmark, let's discuss other alternatives:
Other alternatives to Lodash's merge
function include:
merge
function.Keep in mind that these alternatives might offer different trade-offs between performance, readability, and maintainability, depending on your specific use case and requirements.