Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array filter vs. for loop - with for in 2
(version: 0)
This version declares i once before
Comparing performance of:
Filter vs For vs For 2 vs For in
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [1,2,3,4,5,6,7,8,9,0], farr, i,len;
Tests:
Filter
farr = arr.filter(function(item) { return (item>4); });
For
for (i=0,len=arr.length;i<len;i++) { if (arr[i]<5) continue; farr.push(arr[i]); }
For 2
for (i=0,len=arr.length;i<len;i++) { if (arr[i]>4) farr.push(arr[i]); }
For in
for (i in arr) { if (arr[i]>4) farr.push(arr[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Filter
For
For 2
For in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter
16019515.0 Ops/sec
For
3186558.5 Ops/sec
For 2
3250406.2 Ops/sec
For in
1208180.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested, compared, and other considerations. **Benchmark Definition** The benchmark definition represents a single test case, which in this case is comparing three different approaches to filter an array: 1. `Array.filter()`: A built-in JavaScript method that creates a new array with all elements that pass the provided test. 2. `For loop with explicit index (`: A traditional for loop that iterates over the array using an explicit index (`i`) and checks each element against the condition. 3. `For loop with `in` syntax`: A more concise way to iterate over an object's properties, which can be used to access array elements. **Options Compared** The three options are compared in terms of performance (expressed as executions per second). **Pros and Cons** 1. **Array.filter()**: * Pros: concise, efficient, and well-maintained. * Cons: may not work well with non-array data structures or when the filtering condition is complex. 2. **For loop with explicit index (`)**: * Pros: allows for fine-grained control over iteration and can be used with any data structure. * Cons: more verbose, prone to errors, and less efficient than `filter()` for large datasets. 3. **For loop with `in` syntax**: * Pros: concise, easy to read, and works well with arrays. * Cons: not designed for general-purpose iteration and may not work as expected with non-array data structures. **Library Used** None is explicitly mentioned in the provided JSON data. **Special JS Feature or Syntax** The use of the `in` keyword in the for loop syntax is a special feature of JavaScript that allows iterating over an object's properties. This feature is often used to access array elements, but it's not exclusive to arrays and can lead to unexpected behavior when used with non-array data structures. **Other Considerations** * The benchmark uses Mobile Safari 16 as the test browser, which may have performance characteristics different from other browsers. * The device platform (Mobile) and operating system (iOS 16.1.1) are specified, which may affect the results due to hardware or software-specific factors. * The `ExecutionsPerSecond` value represents the number of iterations performed by each test case within a second. **Alternatives** Other approaches to filtering an array include: 1. Using `map()` and `filter()` together: `[arr.filter(item => item > 4).map(i => i)]` 2. Using `every()` method: `[arr.filter(i => i > 4)]` 3. Using a functional programming approach with `reduce()` and `filter()`: `[arr.reduce((acc, i) => (i > 4 ? acc : [i]), [])]` These alternatives may have different performance characteristics or trade-offs in terms of readability and maintainability.
Related benchmarks:
Iteration through array; of vs forEach
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?