<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script src='https://unpkg.com/deepmerge@4.2.2/dist/umd.js'></script>
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } };
var b = { c: { b: { d: 'a' }, c: { d: 'd' } } };
var c = _.merge({}, a, b);
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } };
var b = { c: { b: { d: 'a' }, c: { d: 'd' } } };
var c = _.merge({}, a, b);
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } };
var b = { c: { b: { d: 'a' }, c: { d: 'd' } } };
var c = deepmerge({}, a, b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
deepmerge |
Test name | Executions per second |
---|---|
lodash | 481645.0 Ops/sec |
deepmerge | 749370.9 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
The benchmark compares the performance of two JavaScript libraries: Lodash and DeepMerge (version 4.2.2). The test case uses two objects, a
and b
, with nested structures, to simulate a common use case for merging two objects.
Options Compared
The benchmark tests the following options:
_merge()
function from Lodash.Pros and Cons of Each Approach
Lodash:
Pros:
Cons:
DeepMerge:
Pros:
Cons:
Library Purpose
The lodash
library is a comprehensive collection of utility functions for functional programming tasks. It provides a wide range of features beyond object merging, such as array manipulation, functional operations, and more.
In the context of this benchmark, Lodash's _merge()
function is used to combine two objects into one. The function takes an optional third argument (the result) and merges objects using a specified strategy (defaulting to deep
if not provided).
The deepmerge
library, on the other hand, is specifically designed for merging objects in a deep, recursive manner without relying on additional libraries like Lodash.
Special JS Feature or Syntax
Neither of the tested libraries utilizes any special JavaScript features or syntax. However, both implementations take advantage of JavaScript's built-in object properties and methods (e.g., Object.assign()
, { property: value }
) to perform their respective merging operations.
Other Alternatives
For those looking for alternative approaches:
_merge()
function similar to Lodash.Keep in mind that each alternative has its own trade-offs and suitability for specific use cases.