Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for continue vs filter
(version: 1)
Comparing performance of:
for continue vs filter
Created:
7 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array(5000).fill(0).concat(Array(5000).fill(1)); var filteredArray = [];
Tests:
for continue
for (item of array) { if(item['id'] > 0.5) continue; filteredArray.push(item) }
filter
for (item of array.filter(el => el['id'] > 0.5)){ filteredArray.push(item) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for continue
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/60.5 Safari/605.1.15
Browser/OS:
Safari 60 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for continue
103.5 Ops/sec
filter
317.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark provided compares two different approaches for filtering elements from an array in JavaScript: a traditional `for` loop with a `continue` statement and the `Array.prototype.filter()` method. ### Benchmark Options 1. **For Loop with Continue** - **Code**: ```javascript for (item of array) { if(item['id'] > 0.5) continue; filteredArray.push(item); } ``` - **Pros**: - This method allows for full control over the iteration. Engineers can easily insert additional logic, manipulate indices, or break out of the loop based on complex conditions. - It can potentially be more efficient in some cases as it avoids the overhead of creating a new array that the `filter` method would require. - **Cons**: - It can result in less readable and more error-prone code, particularly when the logic grows complex. - There's a risk of a higher cognitive load when maintaining the loop’s logic. 2. **Array.prototype.filter** - **Code**: ```javascript for (item of array.filter(el => el['id'] > 0.5)) { filteredArray.push(item); } ``` - **Pros**: - Highly readable and expressive, making it clear that this operation is filtering an array based on a condition. - It allows for cleaner function composition and can help with chaining methods in functional programming paradigms. - **Cons**: - The `filter` method creates a new array, which can introduce an overhead in terms of performance and memory consumption, especially for large datasets, as you are maintaining two arrays in memory. - It may be less efficient in cases where the running environment or the specific context of the dataset makes the overhead unnecessary. ### Additional Considerations - **Performance**: According to the latest benchmark results from Safari 60, the `filter` method executed at approximately 317.79 operations per second, while the `for continue` loop executed at around 103.48 operations per second. This indicates that the `filter` method is significantly faster under the given testing conditions, which may be surprising given the traditional assumption that manual loops are always more performant. - **Readability vs. Performance**: The choice between these two methods often boils down to a trade-off between readability and raw performance. In many situations, using `filter` enhances code clarity at the cost of potential performance degradation with larger datasets. However, with the results showing `filter` as the faster option in this benchmark, it may be prudent to prefer `filter` unless profiling shows otherwise. ### Alternatives - **Using `forEach`**: Another commonly used approach would be to use `Array.prototype.forEach()` to iterate over the array and conditionally push elements. However, `forEach` does not allow for early exits with `break` or `continue`. - **Traditional `for` Loop**: A classic `for` loop (for example, `for (let i = 0; i < array.length; i++)`) could be used, which provides maximum flexibility to control the iteration but may lead to similar readability concerns as the first approach. - **Libraries such as Lodash**: Employing utility libraries like Lodash can provide methods like `_.filter` which is a syntactically cleaner way to filter arrays while still being performant. Overall, choosing the right approach should consider specific use cases, team standards, and the environment in which the code will run.
Related benchmarks:
Array_Filter_VS_loop
Array filter vs. for loop - with for in 2
Slice vs Filter (1111122222)
Slice vs Filter (2222211111)-4
Some, Filter, indexOf
Some, Filter, indexOf.
Compare performance of for..of and forEach with filter, v3
JS filter or forEach
Array filtering performance
Comments
Confirm delete:
Do you really want to delete benchmark?