Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
`filter().length` vs. `reduce` vs. `for`
(version: 0)
Compare `filter().length` vs `reduce` vs `for` loop for counting array elements that are present in another array.
Comparing performance of:
filter().length vs reduce vs for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var sample = Array(1000).fill(0).map((_, idx) => idx); var selected = [10, 134, 245, 355, 489, 583, 682, 823, 2081, 3892, 4892, 5829, 6832];
Tests:
filter().length
var total = selected.filter(id => sample.includes(id)).length;
reduce
var total = selected.reduce((total, id) => total + (sample.includes(id) ? 1 : 0), 0);
for loop
var total = 0; for (const id of selected) { if (sample.includes(id)) { total++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
filter().length
reduce
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter().length
655499.4 Ops/sec
reduce
684719.1 Ops/sec
for loop
688710.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark defines three test cases: 1. `filter().length` 2. `reduce` 3. `for` loop These three approaches are used to count the number of elements in the `selected` array that are also present in the `sample` array. **Options Compared** Each option is compared in terms of execution time, measured in executions per second (ExecutionsPerSecond). * `filter().length`: This approach uses the `filter()` method to create a new array with only the elements from `selected` that are present in `sample`. The `length` property then returns the number of elements in this filtered array. * `reduce`: This approach uses the `reduce()` method to iterate over each element in `selected`, and for each element, checks if it is present in `sample`. If it is, a counter (initially set to 0) is incremented by 1. The final result is the total count of elements present in both arrays. * `for` loop: This approach uses a traditional `for` loop to iterate over each element in `selected`, and for each element, checks if it is present in `sample`. If it is, an increment counter variable (`total`) is incremented by 1. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: * `filter().length`: + Pros: concise and efficient way to filter out unwanted elements. + Cons: creates a new array and may not be suitable for very large datasets due to memory concerns. * `reduce`: + Pros: can handle large datasets without creating intermediate arrays, and is often considered more functional programming-style. + Cons: can be slower than other approaches due to the overhead of the reduce function, and may require understanding of how it works. * `for` loop: + Pros: straightforward and easy to understand for those familiar with traditional loops. + Cons: may not be as concise or efficient as other approaches, especially for large datasets. **Library Used** The benchmark does not explicitly mention any libraries used. However, the `filter()` and `reduce()` methods are built-in JavaScript functions that operate on arrays. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax beyond standard ECMAScript 2015+ (ES6+) syntax. It relies solely on basic array operations and control structures. **Other Alternatives** Some alternative approaches to counting the number of elements in `selected` that are also present in `sample` might include: * Using a Set data structure, which provides faster membership testing than arrays. * Using a library like Lodash or Moment.js, which provide utility functions for array manipulation and filtering. * Using a custom implementation with bitwise operations to check for presence. However, these alternatives would likely change the nature of the benchmark and may not be directly comparable to the original `filter()`, `reduce()`, and `for` loop approaches.
Related benchmarks:
`filter().length` vs. `reduce` vs. `for` (with Set)
Filter and Map vs Reduce
Count matches using Array reduce vs Array filter
`filter().length` vs. `reduce`
Comments
Confirm delete:
Do you really want to delete benchmark?