<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
<script src='https://unpkg.com/deepmerge@3.2.0/dist/umd.js'></script>
var fp = _.noConflict();
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 = fp.merge(b, a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash merge | |
object.assign | |
lodash/fp merge |
Test name | Executions per second |
---|---|
lodash merge | 157821.9 Ops/sec |
object.assign | 967923.8 Ops/sec |
lodash/fp merge | 143218.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is tested?
On MeasureThat.net, users can create and run JavaScript microbenchmarks to compare different approaches for merging objects. The benchmark tests three popular libraries: Lodash, Deepmerge, and Lodash/FP (Functional Programming).
The individual test cases demonstrate how each library merges two objects, a
and b
, using the following syntax:
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } };
var b = { c: { b: { d: 'a' }, c: { d: 'd' } } };
The test cases are:
_.merge()
: var c = _.merge({}, a, b);
var c = object.assign({}, a, b);
(not explicitly shown in the benchmark code, but implied)fp.merge()
: var c = fp.merge(b, a);
Options compared
The benchmark compares three different approaches for merging objects:
_.merge()
): Lodash is a utility library that provides a wide range of functions for manipulating data, including object merging. The _merge()
function takes multiple arguments and returns a new object with the merged properties.fp.merge()
): Lodash/FP is a functional programming variant of Lodash, which provides a different set of functions for manipulating data.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
_.merge()
):_.merge()
.fp.merge()
):Library explanations
_.merge()
function is an essential part of the library.Special JS features
The benchmark doesn't explicitly mention any special JavaScript features or syntax. However, it's worth noting that some of these libraries (e.g., Lodash) might require additional setup or configuration, such as importing dependencies using ES modules or commonJS.
Alternatives
Other alternatives for merging objects in JavaScript include:
{ ...a, ...b }
): This is a shorthand way to merge two objects using the spread operator.In conclusion, MeasureThat.net's benchmark allows users to compare different approaches for merging objects in JavaScript, providing valuable insights into their performance characteristics. By understanding the pros and cons of each approach, developers can make informed decisions about which solution best suits their specific use case.