Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.merge vs _.assign vs _.reduce vs JS reduce vs Js assign
(version: 0)
Comparing performance of:
Lodash merge vs Lodash assign vs Js reduce vs Js assign vs _.reduce
Created:
2 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>
Script Preparation code:
var array = Array(1000).fill().map((_, index) => ({ [(Math.random() + 1).toString(36).substring(2)]: index }))
Tests:
Lodash merge
_.merge({}, ...array)
Lodash assign
_.assign({}, ...array)
Js reduce
array.reduce((acc, item) => ({...acc, ...item}), {})
Js assign
Object.assign({}, ...array)
_.reduce
_.reduce((acc, item) => ({...acc, ...item}), {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Lodash merge
Lodash assign
Js reduce
Js assign
_.reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash merge
8989.4 Ops/sec
Lodash assign
10086.4 Ops/sec
Js reduce
20.7 Ops/sec
Js assign
33492.1 Ops/sec
_.reduce
5850542.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided JSON represents a benchmark that compares four different approaches to merge or manipulate an array in JavaScript: 1. `_.merge` from Lodash 2. `_.assign` from Lodash 3. `array.reduce((acc, item) => ({...acc, ...item}), {})` 4. `Object.assign({}, ...)` **Approaches Compared** The four approaches are compared in terms of performance. The benchmark creates an array with 1000 objects, each containing a random property with a unique value. The approaches then merge or manipulate this array to produce a new object. * `_`. `merge` and `_.assign` are two functions from the Lodash library that perform deep merging of objects. * `array.reduce((acc, item) => ({...acc, ...item}), {})` uses the `reduce` method on the array to merge the objects. This approach accumulates the result by reducing the array to a single object. * `Object.assign({}, ...)`, also known as the "spread operator" or "rest operator," is used to create a new object with the merged properties. **Pros and Cons of Each Approach** 1. **Lodash `merge` and `_assign`**: * Pros: These functions are highly optimized for performance and provide a robust way to merge objects. * Cons: They require an external library (Lodash), which may not be desirable for small projects or those with size constraints. 2. **`array.reduce`**: * Pros: This approach is lightweight and doesn't require any external libraries. * Cons: It can be slower due to the overhead of the `reduce` method, especially for large arrays. 3. **`Object.assign`** (spread operator): * Pros: This approach is simple, lightweight, and widely supported across browsers and Node.js environments. * Cons: It may not work as expected if the input array contains non-objects or if the objects have different property types. **Other Considerations** When choosing an approach, consider the following factors: * Performance: If speed is critical, Lodash `merge` and `_assign` are likely to be faster. * Size constraints: If you're working on a project with size constraints, using an external library like Lodash might not be feasible. * Browser support: If you need to support older browsers, the spread operator (`Object.assign`) may not work as expected. **Library Used (if applicable)** In this benchmark, two libraries are used: 1. **Lodash**: Provides the `merge` and `_assign` functions used in the benchmark. 2. **No external library is required for the `array.reduce` approach**, which uses only built-in JavaScript features. **Special JS Features/Syntax** None of the approaches use special JavaScript features or syntax that would impact their performance or behavior. The benchmark focuses on comparing well-established methods for merging and manipulating arrays in JavaScript.
Related benchmarks:
Loop over object: lodash vs Object.entries fork by d9k 2
Loop over object: lodash vs Object.entries fork by d9k 3
test123_123
_.merge vs _.assign vs JS reduce
Comments
Confirm delete:
Do you really want to delete benchmark?