Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.chain then reduce vs for
(version: 0)
Comparing performance of:
Lodash reduce vs Native reduce
Created:
5 years ago
by:
Registered User
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 users = {} for (var i = 0; i < 1000; i++) { if (i % 2) { users[i] = {a: "a", b: "b"} } else { users[i] = {b: "b"} } }
Tests:
Lodash reduce
var res = _.chain(users) .reduce((acc, a, userID) => { if (a === "a") { acc.push(userID) } return acc }, []) .value()
Native reduce
var res = [] for (key in users) { if (users[key].a) res.push(key) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash reduce
Native reduce
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 JSON benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing two approaches to filter an array of objects in JavaScript: 1. Using Lodash's `chain` method with `reduce` 2. Using a traditional `for` loop with object iteration **Options Compared** In this benchmark, we have two options being compared: * **Lodash reduce**: This approach uses the `_chain` function from Lodash to create a pipeline of operations on the `users` array. The `reduce` method is then applied to the pipeline, which iterates over each object in the array and checks if its `a` property is equal to `"a"`. If it is, the `userID` is added to an accumulator array. * **Native reduce**: This approach uses a traditional `for` loop with object iteration to achieve the same result as Lodash's `reduce`. The loop iterates over each key in the `users` object and checks if its value has an `a` property. If it does, the key is added to an accumulator array. **Pros and Cons** Here are some pros and cons of each approach: * **Lodash reduce**: + Pros: - Concise and readable code - Easy to maintain and modify + Cons: - Adds overhead due to Lodash library inclusion - May be slower due to pipeline creation and iteration * **Native reduce**: + Pros: - No additional library dependencies - May be faster due to direct access to the `users` object + Cons: - More verbose code - Less readable due to complex loop logic **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for common tasks, such as array manipulation, string processing, and more. The `_chain` function creates a pipeline of operations on an array or object, allowing you to chain multiple methods together. In this benchmark, `_.chain` is used to create a pipeline that can be passed to the `reduce` method. **Special JS Feature: none** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** If you're interested in alternative approaches to filtering arrays in JavaScript, here are a few options: * Using `filter()` with an arrow function * Using `forEach()` and pushing elements to an array * Using `map()` and creating a new array with filtered elements Each of these alternatives has its own pros and cons, but they can provide different trade-offs in terms of code readability, performance, and maintainability.
Related benchmarks:
lodash forEach vs reduce
lodash vs es6 in reduce method
Loop over object: lodash vs Object.entries fork by d9k 2
Loop over object: lodash vs Object.entries fork by d9k 3
test123_123
Comments
Confirm delete:
Do you really want to delete benchmark?