Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs foreach vs for
(version: 0)
Comparing performance of:
double filter vs forEach vs for
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(i=0; i<1000; i++){ Math.random() > .5 ? arr.push({is_group: true}) : arr.push({is_group: false}) }
Tests:
double filter
const a = arr.filter(i => i.is_group)
forEach
const a = []; arr.forEach(i => { if (i.is_group) { a.push(i) } })
for
const a = []; for(i=0; i<1000; i++){ if (arr[i].is_group) { a.push(arr[i]) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
double filter
forEach
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
double filter
249964.7 Ops/sec
forEach
96479.1 Ops/sec
for
15492.5 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. **Benchmark Definition** The benchmark is comparing three approaches to filter an array in JavaScript: 1. `filter()` 2. `forEach()` with a conditional statement 3. `for` loop with a conditional statement **Options Compared** Here are the options being compared, along with their pros and cons: * **Filter()** + Pros: concise syntax, efficient (on average), easy to read. + Cons: can be slower for large arrays due to the overhead of creating an iterator object. * **ForEach() with conditional statement** + Pros: allows for more flexibility in filtering, can be easier to understand than `filter()` for some cases. + Cons: requires additional logic and checks, which can lead to slower performance. * **For loop with conditional statement** + Pros: provides direct control over iteration and filtering, can be faster for large arrays due to the overhead of function calls. + Cons: more verbose syntax, harder to read. **Library Usage** None of the options use a library explicitly in this benchmark. However, it's worth noting that `filter()` uses an internal implementation that creates an iterator object, which might be relevant for libraries or frameworks that optimize JavaScript performance. **Special JS Feature/Syntax** There are no special JS features or syntax used in this benchmark. The test cases only use standard JavaScript syntax and built-in methods. **Other Considerations** When choosing between these options, consider the following factors: * Code readability: `filter()` is often the most concise and readable choice. * Performance: For large arrays, `for` loops might be faster due to the overhead of function calls. However, this can depend on the specific use case and implementation. * Control: If you need more control over iteration and filtering, a `for` loop with conditional statements might be a better choice. **Alternatives** Other alternatives for filtering arrays in JavaScript include: * `map()`: similar to `filter()`, but returns a new array instead of modifying the original one. * `every()` and `some()`: can be used to filter arrays, but often require more complex logic and are less concise than `filter()`. In summary, this benchmark is testing three common approaches to filtering an array in JavaScript: `filter()`, `forEach()` with a conditional statement, and `for` loop with a conditional statement. Each option has its pros and cons, and the choice ultimately depends on code readability, performance, and control requirements.
Related benchmarks:
double filter vs foreach
double filter vs foreach2
double-filter vs foreach vs for
Array.prototype.some() vs. Filter vs. Array.prototype.indexOf()
Comments
Confirm delete:
Do you really want to delete benchmark?