<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script src='https://unpkg.com/deepmerge@3.2.0/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 = deepmerge({}, 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 = Object.assign({}, a, b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash merge | |
deepmerge | |
object.assign |
Test name | Executions per second |
---|---|
lodash merge | 642300.5 Ops/sec |
deepmerge | 4661731.0 Ops/sec |
object.assign | 28095542.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach.
Overview The benchmark is designed to compare the performance of three functions:
Object.assign()
deepmerge()
lodash.merge()
These functions are used to merge two objects into one. The test cases use similar input data with slight variations, allowing us to isolate the differences in performance between these libraries.
What's being tested
The benchmark is testing how efficient each library is when merging objects. Specifically, it's measuring:
Options compared
Object.assign()
: A built-in JavaScript function that merges two or more source objects into a new object.deepmerge()
: A third-party library that provides a deep merge function, allowing you to merge objects recursively.lodash.merge()
: A utility function from the Lodash library that performs a deep merge of two objects.Pros and Cons
Object.assign()
:deepmerge()
:lodash.merge()
:deepmerge()
.Library explanations
deepmerge
: A small library created by Andrew Whitehurst that provides a simple and efficient way to merge objects recursively. It's designed for use cases where deep merging is necessary, such as when working with complex data structures.lodash.merge()
: A part of the Lodash utility library, which includes many functions for common tasks like array manipulation, object creation, and more. The merge()
function is specifically designed to handle deep merges of objects.Special JS features or syntax
None are mentioned in this benchmark.