<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var max = 100000;
var arr = [];
for (var i = 0; i <= max; i++) { arr.push(i); }
arr.forEach(() => {
let object = { 'a': { 'b': 2 } };
let other = { 'a': { 'b': 1, 'c': 3 } };
_.defaultsDeep(object, other);
})
function defaultsDeep(target, sources) {
sources.forEach(source => {
Object.keys(source).forEach(key => {
if (typeof source[key] === 'object') {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
defaultsDeep(target[key], source[key]);
} else {
if (!target[key]) {
Object.assign(target, { [key]: source[key] });
}
}
});
});
return target;
}
arr.forEach(() => {
let object = { 'a': { 'b': 2 } };
let other = { 'a': { 'b': 1, 'c': 3 } };
defaultsDeep(object, other);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native |
Test name | Executions per second |
---|---|
Lodash | 34.5 Ops/sec |
Native | 39.4 Ops/sec |
Let's dive into the benchmark.
Benchmark Overview
The benchmark compares two approaches for merging objects: Lodash's defaultsDeep
function and JavaScript's built-in Object.assign
. The test creates an array of 100,000 elements, where each element represents an object with a nested structure. One object is used as the target (object
), and another object is used as the source (other
). The benchmark measures which approach performs better in merging these two objects.
Options Compared
The two options being compared are:
defaultsDeep
function: This function is a utility that merges two objects recursively, creating a new object with values from both sources. It handles nested objects and arrays.Object.assign
: This method creates a new object by copying all enumerable properties from one or more source objects.Pros and Cons
defaultsDeep
function:Object.assign
:In general, Lodash's defaultsDeep
function is a better choice when:
On the other hand, JavaScript's built-in Object.assign
might be preferred when:
Library: Lodash
Lodash is a popular utility library for JavaScript that provides various functions for tasks like string manipulation, array and object manipulation, and more. defaultsDeep
is one of the many useful functions in the library.
Special JS Feature/Syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond what's required for the merge operations. However, it's worth noting that some versions of JavaScript (like ECMAScript 2015+) have added new features like Object.entries
and Object.fromEntries
, which might affect performance in certain cases.
Alternatives
If you're looking for alternatives to Lodash or prefer a more lightweight solution, consider the following options:
defaultsDeep
, although it's not as widely used as Lodash.Object.assign
with reduce
or forEach
: While this approach is not as lightweight as using a dedicated library, it can be an alternative solution for simple object merges.Keep in mind that these alternatives might have different performance characteristics and use cases compared to the benchmarked approaches.