Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs. handwired for loop filtering
(version: 0)
Comparing performance of:
Array_filter_method vs classic_for_loop_filter
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array({ length: 10000 }, () => Math.floor(Math.random() * 1000));
Tests:
Array_filter_method
var isGreaterThan500 = val => val > 500; var filtered = arr.filter(isGreaterThan500)
classic_for_loop_filter
var l = arr.length var filtered = []; for (var i = 0; i < l; i++) { var val = arr[i]; if (val > 500) { filtered.push(val); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array_filter_method
classic_for_loop_filter
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array_filter_method
5464414.5 Ops/sec
classic_for_loop_filter
3971685.0 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. **Overview** The benchmark compares two approaches to filter an array of numbers: using the `Array.prototype.filter()` method (which is often referred to as "filtering") and implementing a handwired for loop to achieve the same filtering result. **Test Cases** There are two test cases: 1. **`Array_filter_method`**: This test case uses the built-in `Array.prototype.filter()` method to filter the array. 2. **`classic_for_loop_filter`**: This test case implements a handwired for loop to filter the array, which means manually iterating over each element and adding it to an output array if the condition is met. **Comparison** The benchmark compares the execution time of these two approaches on a large array (10,000 elements) generated with JavaScript's `Array()` constructor. **Options Compared** * **Filtering using `Array.prototype.filter()`**: This approach uses a built-in method to filter the array. * **Handwired for loop filtering**: This approach manually iterates over each element in the array and adds it to an output array if the condition is met. **Pros and Cons of Each Approach** * **`Array.prototype.filter()`**: + Pros: Fast, concise, and easy to read. It's a built-in method that's optimized for performance. + Cons: May not be as efficient in extremely large arrays or specific edge cases (e.g., very large integers). * **Handwired for loop filtering**: + Pros: Can be more flexible and suitable for extreme cases where the built-in filter is not sufficient. + Cons: More verbose, error-prone, and less readable compared to the `Array.prototype.filter()` method. **Library Usage** There are no external libraries used in this benchmark. The `Array` constructor and basic JavaScript operators (e.g., `>`, `push`) are part of the language itself. **Special JS Features or Syntax** None mentioned. **Other Alternatives** If you're looking for alternative approaches to filtering arrays, some other options include: * Using `Array.prototype.reduce()` with a custom callback function. * Implementing a custom sorting algorithm and then selecting every nth element (e.g., using `Array.prototype.slice()`). * Utilizing libraries like Lodash or Ramda, which provide optimized filtering functions. Keep in mind that the best approach will depend on the specific use case, performance requirements, and personal preference.
Related benchmarks:
Some vs. Filter vs. indexOf 1
Array.prototype.some() vs. Filter vs. Array.prototype.indexOf()
Array filter vs. for loop - with for in zz
Array filter vs. for loop - with for in (10k)
Comments
Confirm delete:
Do you really want to delete benchmark?