Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array filter vs. for loop (fixed2)
(version: 0)
Comparing performance of:
Filter vs For vs For 2
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1,2,3,4,5,6,7,8,9,0], farr;
Tests:
Filter
farr = arr.filter(function(item) { return (item>4); } );
For
for (var i=0;i<arr.length;i++) { if (arr[i]<5) continue; farr.push(arr[i]); }
For 2
for (var i=0;i<arr.length;i++) { if (arr[i]>4) farr.push(arr[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Filter
For
For 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The test measures the performance difference between three approaches to filter an array in JavaScript: 1. `Array.filter()` 2. `for` loop with a conditional statement 3. `for` loop without a conditional statement (the second one is similar to the first, but without filtering) **Script Preparation Code** The script preparation code sets up an array `arr` containing 10 elements and creates a variable `farr` that will be used as the output array. ```javascript var arr = [1,2,3,4,5,6,7,8,9,0], farr; ``` **Html Preparation Code** There is no HTML preparation code provided, which means this benchmark only focuses on JavaScript performance. **Test Cases** The three test cases are: 1. **Filter**: This test uses the `Array.filter()` method to create a new array that includes only elements greater than 4. ```javascript farr = arr.filter(function(item) { return (item>4); }); ``` 2. **For**: This test uses a traditional `for` loop with an if statement to push elements into the output array `farr`. ```javascript for (var i=0;i<arr.length;i++) { if (arr[i]<5) continue; farr.push(arr[i]); } ``` 3. **For 2**: This test is similar to the previous one, but without the conditional statement (`if (arr[i]<5)`). It simply pushes all elements into the output array `farr`. ```javascript for (var i=0;i<arr.length;i++) { farr.push(arr[i]); } ``` **Library: `Array.prototype.filter()`** The `Array.prototype.filter()` method is a built-in JavaScript method that creates a new array with only the elements that pass the test implemented by the provided function. In this case, the filter function checks if an element is greater than 4. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in these tests. They are straightforward implementations of basic programming concepts. **Alternatives** Other approaches to filtering arrays could include: * Using `Array.prototype.every()` method with a callback function * Using `Array.prototype.map()` method followed by `Array.prototype.slice()` * Using `Array.prototype.reduce()` method (although this would be less efficient) These alternatives might be interesting for further benchmarking, but they are not the focus of this specific test case.
Related benchmarks:
Array filter vs. for loop - with for in 2
Array filter vs. for loop and for in
Array filter vs. for loop (fixed)2
Array filter vs. for loop (fixedN)
Comments
Confirm delete:
Do you really want to delete benchmark?