Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for with continue vs for over a .filter (v2)
(version: 1)
Comparing performance of:
for with continue vs for with .filter
Created:
7 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const array = new Array(100).fill(1).map((_,index) =>index)
Tests:
for with continue
let sum = 0 for (const el of array){ if (el % 2 !== 0) continue; sum =+ el; }
for with .filter
let sum = 0 for (const el of array.filter(x => x % 2 === 0)){ sum =+ el }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for with continue
for with .filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for with continue
1160363.4 Ops/sec
for with .filter
895804.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark compares two different approaches for iterating over an array and summing even numbers in JavaScript: ### Approaches Compared: 1. **For Loop with `continue`:** - **Code:** ```javascript let sum = 0; for (const el of array) { if (el % 2 !== 0) continue; sum =+ el; } ``` - **Test Name:** "for with continue" 2. **For Loop using `.filter()`:** - **Code:** ```javascript let sum = 0; for (const el of array.filter(x => x % 2 === 0)) { sum =+ el; } ``` - **Test Name:** "for with .filter" ### Pros and Cons: #### For Loop with `continue`: - **Pros:** - **Performance:** This approach can be more efficient since it checks each element during the iteration, skipping only the odd ones without creating a new array. - **Memory Usage:** It doesn't require additional memory for an intermediate array, as it processes elements in place. - **Cons:** - **Readability:** It might be less clear than using a function like `filter`, particularly for developers who are less familiar with control flows in loops. - **Complexity:** For more complex conditions, the loop can become harder to read and maintain. #### For Loop using `.filter()`: - **Pros:** - **Readability:** The use of `filter` makes it more declarative, which can enhance readability and understanding of the intent behind the code. - **Functional Style:** Appeals to developers familiar with functional programming, leveraging JavaScript's higher-order functions. - **Cons:** - **Performance:** Creating a new array for the filtered results incurs overhead, potentially leading to slower execution times, especially for large arrays. - **Memory Usage:** Requires additional memory for storing the filtered array before the summation occurs. ### Library and Features: - **Library/Feature Used:** The code does not make use of any external libraries; instead, it relies on built-in JavaScript features, specifically the Array method `.filter()`. - **Purpose of `.filter()`:** This method creates a new array with all elements that pass the test implemented by the provided function. In this case, it checks for even numbers. ### Alternative Approaches: - **Using `reduce()`:** One could also use the `reduce()` method to accumulate the sum of even numbers in a single pass, which would eliminate the need for a separate filtering step or the use of the `continue` statement. ```javascript const sumEven = array.reduce((sum, el) => el % 2 === 0 ? sum + el : sum, 0); ``` - **Manual Looping:** Combining traditional `for` loop structures or leveraging `while` loops can also be considered, offering potentially optimized performance for specific cases. ### Conclusion: This benchmark illustrates the trade-offs between different coding styles in JavaScript when it comes to operations on arrays. The choice of method should depend on the specific use case, including performance requirements, the size of the data set, and the importance of code readability and maintainability.
Related benchmarks:
for vs foreach vs map vs (map, filter reduce)
forEach vs reduce vs for vs map and filter
Storing array length before for
for/for in/forEach array sum
for/for in/forEach array sum 222
for/for len
forEach vs reduce vs map vs filter vs for (to build an array)
For let vs var vs const
for with continue vs for over a .filter
Comments
Confirm delete:
Do you really want to delete benchmark?