Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
merge vs mergeWith vs object.assign vs spread12 vs test
(version: 0)
Comparing performance of:
lodash merge vs object.assign vs spread vs lodash mergeWith vs My test
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Tests:
lodash merge
var a = { a: 'oh', b: 'my', d: 'ss' }; var b = { c: 'goddess', d: 's2s' }; var c = _.merge(a, b); console.log('merge', c)
object.assign
var a = { a: 'oh', b: 'my', d: 'ss' }; var b = { c: 'goddess', d: 's2s' }; var c = Object.assign(a, b); console.log('assign', c)
spread
var a = { a: 'oh', b: 'my', d: 'ss' }; var b = { c: 'goddess', d: 's2s' }; var c = { ...a, ...b }; console.log('spread', c)
lodash mergeWith
var a = { a: 'oh', b: 'my', d: 'ss' }; var b = { c: 'goddess', d: 's2s' }; var c = _.mergeWith(a, b); console.log('mergeWith', c)
My test
var a = { a: 'oh', b: 'my', d: 'ss' }; var b = { c: 'goddess', d: 's2s' }; const customMerge = (a, b) => { const result = Object.assign({}, a, b); Object.keys(result).forEach(key => { if (typeof a[key] !== 'object' || typeof b[key] !== 'object') { return; } Object.assign(result, { [key]: Object.assign(a[key], b[key]) }); }); return result; }; var c = customMerge(a, b); console.log('customMerge', c)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
lodash merge
object.assign
spread
lodash mergeWith
My test
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares four methods for merging objects: `lodash.merge`, `object.assign`, `spread` (using the rest/spread syntax), and a custom implementation `customMerge`. Each test case uses a similar input object `{a: 'oh', b: 'my', d: 'ss'}` and another object `{c: 'goddess', d: 's2s'}`. **What is being tested?** The main goal of the benchmark is to compare the performance, efficiency, and possibly other factors (like memory usage or readability) of each merge method. The test cases aim to identify which method is the fastest, most efficient, or a good trade-off between these factors. **Options compared:** 1. **Lodash's `merge`**: A popular JavaScript library function for merging objects. 2. **Object.assign()**: A built-in JavaScript method for assigning values from one object to another. 3. **Spread syntax ( `{...a, ...b}` )**: A shorthand way of creating a new object by spreading the properties of two existing objects. 4. **Custom implementation (`customMerge`)**: A custom function that manually merges two objects using `Object.assign()` and recursion. **Pros and Cons:** 1. **Lodash's `merge`**: * Pros: + Widely used and well-maintained library. + Handles nested objects, arrays, and even functions. * Cons: + Adds an external dependency (Lodash). + Can be slower due to the overhead of a function call. 2. **Object.assign()**: * Pros: + Built-in method, no extra dependencies needed. + Fast and lightweight. * Cons: + Limited control over the merge process. + Doesn't handle nested objects or arrays well without additional workarounds. 3. **Spread syntax ( `{...a, ...b}` )**: * Pros: + Simple, concise, and easy to read. + Fast and lightweight. * Cons: + Limited control over the merge process. + Only works with objects; not suitable for arrays or functions. 4. **Custom implementation (`customMerge`)**: * Pros: + Provides complete control over the merge process. + Can be optimized for specific use cases (e.g., handling nested objects). * Cons: + More complex and error-prone to implement correctly. + May introduce performance overhead due to recursive function calls. **Library usage:** The benchmark uses Lodash's `merge` function, which is a popular JavaScript library. The purpose of using this library is likely to demonstrate its strengths and weaknesses in the context of object merging. **Special JS feature/syntax:** The benchmark doesn't explicitly use any special JavaScript features or syntax, such as ES6 classes, async/await, or decorators. However, it does utilize modern JavaScript features like template literals (`"var c = { ...a, ...b };"`). **Alternatives:** Other alternatives for merging objects in JavaScript include: 1. **DeepAssign**: A library specifically designed for deep object merging. 2. **obj-cp**: Another library for copying and merging objects. 3. **Using recursive functions**: Implementing a custom merge function using recursion, like `customMerge`. 4. **Native `JSON.parse()` and `JSON.stringify()` methods**: While not ideal for merging complex objects, these methods can be used for simple cases. In summary, the benchmark aims to compare the performance and efficiency of four different methods for merging objects in JavaScript: Lodash's `merge`, `object.assign()`, spread syntax, and a custom implementation.
Related benchmarks:
lodash merge vs object.assign vs spread new obj
lodash.assign vs object.assign vs spread
lodash merge vs object.assign vs spread overwriting one property
lodash merge vs object.assign vs spread (no intermediate vars)
lodash assign vs object.assign vs spread operator - variable and constant
Comments
Confirm delete:
Do you really want to delete benchmark?