Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.map().filter(Boolean) vs Array.flatMap() vs Array.reduce()
(version: 0)
comparison of filtering out falsy values from an array
Comparing performance of:
Array.map().filter(Boolean) vs Array.flatMap() vs Array.reduce()
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a1 = ['', '1231237', '', '', '', '', 'ergre', '', '', '', '', 'asdf1134', '', '', '123']; var a2 = ['1231237', '123', '1231237', '123', '1231237', '', '123sdfsdf', 'sdfsdf'], result, a1 = a1.flatMap((s, i) => i % 2 ? a1 : a2), a2 = a2.flatMap((s, i) => i % 2 ? a2 : a1);
Tests:
Array.map().filter(Boolean)
result = a1.map(s => a2.includes(s) && s).filter(Boolean);
Array.flatMap()
result = a1.flatMap(s => s && a2.includes(s) ? [s] : []);
Array.reduce()
result = a1.reduce((acc, s) => (s && a2.includes(s) ? [...acc, s] : acc), []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.map().filter(Boolean)
Array.flatMap()
Array.reduce()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.map().filter(Boolean)
274773.3 Ops/sec
Array.flatMap()
276025.7 Ops/sec
Array.reduce()
45322.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark compares three different approaches to filter out falsy values from an array: 1. `Array.map().filter(Boolean)` 2. `Array.flatMap()` 3. `Array.reduce()` **What is being tested?** Each test case measures the performance of a specific approach in filtering out falsy values from an array. **Options compared** The three options are compared as follows: * `Array.map()` with a conditional filter (`filter(Boolean)`) to remove falsy values * `Array.flatMap()` without any filters, which would return all elements regardless of truthiness * `Array.reduce()` with an accumulator that checks for falsy values and includes only true ones **Pros and Cons of each approach** 1. **`Array.map().filter(Boolean)`** * Pros: + Simple and concise syntax + Easy to understand and maintain * Cons: + May incur additional overhead due to the filter operation + Not as efficient as other approaches for large arrays 2. **`Array.flatMap()`** * Pros: + More concise than `map()` with a conditional filter + Can be more efficient for certain use cases (e.g., when returning an array of values) * Cons: + Returns all elements, regardless of truthiness, which may lead to performance issues for large arrays + May not be suitable for filtering out falsy values directly 3. **`Array.reduce()`** * Pros: + Can be more efficient than `map()` or `flatMap()` by avoiding unnecessary operations + Allows for a flexible accumulator function to filter out falsy values * Cons: + More complex syntax, which can make it harder to understand and maintain + May require additional logic to handle edge cases **Library usage** None of the benchmark cases uses any external libraries. **Special JS feature/syntax** The benchmark uses `flatMap()` and `reduce()`, which are relatively modern JavaScript features. However, since most browsers support these features, they don't pose a significant challenge for older browsers or those that have not adopted them yet. In summary, this benchmark provides a clear comparison of three different approaches to filtering out falsy values from an array, highlighting the trade-offs between simplicity, efficiency, and readability.
Related benchmarks:
Array.map().filter(Boolean) vs Array.flatMap()
flatMap vs reduce filtering performance
Flat map + filter vs. Reduce
flatMap vs reduce flattern array
Comments
Confirm delete:
Do you really want to delete benchmark?