Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loop count
(version: 0)
Comparing performance of:
filter vs foreach vs for
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
filter
var obj1 = {test1: [{prop1: 'test1', enabled: true}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: true}], test2: [{prop1: 'test1', enabled: false}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: true}, {prop1: 'test4', enabled: true}], test3: [{prop1: 'test1', enabled: false}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: false}, {prop1: 'test4', enabled: true}, {prop1: 'test4', enabled: false}]}; var count = 0; for(var key in obj1) { if(obj1.hasOwnProperty(key)){ count += obj1[key].filter(p => p.enabled).length; } }
foreach
var obj1 = {test1: [{prop1: 'test1', enabled: true}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: true}], test2: [{prop1: 'test1', enabled: false}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: true}, {prop1: 'test4', enabled: true}], test3: [{prop1: 'test1', enabled: false}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: false}, {prop1: 'test4', enabled: true}, {prop1: 'test4', enabled: false}]}; var count = 0; for(var key in obj1) { if(obj1.hasOwnProperty(key)){ obj1[key].forEach(p => { if(p.enabled) { ++count; } }) } }
for
var obj1 = {test1: [{prop1: 'test1', enabled: true}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: true}], test2: [{prop1: 'test1', enabled: false}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: true}, {prop1: 'test4', enabled: true}], test3: [{prop1: 'test1', enabled: false}, {prop1: 'test2', enabled: false}, {prop1: 'test3', enabled: false}, {prop1: 'test4', enabled: true}, {prop1: 'test4', enabled: false}]}; var count = 0; for(var key in obj1) { if(obj1.hasOwnProperty(key)){ for(var i = 0; i < obj1[key].length; i++) { if(obj1[key][i].enabled) { ++count; } } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
filter
foreach
for
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/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
filter
11347685.0 Ops/sec
foreach
12052115.0 Ops/sec
for
11850707.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark consists of three test cases that measure the performance of different loops in JavaScript: 1. `for` loop 2. `forEach` loop 3. Filter function with `hasOwnProperty` Each test case creates a large object (`obj1`) and iterates over its properties using one of the mentioned loop constructs. **Loop Comparison** The three tests compare the performance of: * **Filter loop**: The test uses an array filter method to count the number of enabled properties in each property's array. * **ForEach loop**: The test uses a `forEach` loop to iterate over the arrays and count the number of enabled properties. * **For loop**: The test uses a traditional `for` loop with a counter variable to iterate over the arrays and count the number of enabled properties. **Pros and Cons** 1. **Filter loop**: * Pros: Simple and concise code, leverages built-in array methods for performance. * Cons: May have overhead due to the filter method's execution plan. 2. **ForEach loop**: * Pros: Concise and expressive code, easy to read and maintain. * Cons: May have slower performance compared to traditional `for` loops due to the function invocation overhead. 3. **For loop**: * Pros: Traditional looping pattern with known performance characteristics. * Cons: More verbose code, may not be as concise or expressive. **Library Usage** None of the test cases use any external libraries beyond standard JavaScript features. **Special JS Features/Syntax** The tests do not explicitly utilize any special JavaScript features or syntax. However, it's worth noting that `forEach` loops and array methods like filter are modern JavaScript constructs introduced in ECMAScript 5 (ES5). **Alternatives** Other alternatives to these loop constructs include: 1. **Traditional `for` loop**: As mentioned earlier, a traditional `for` loop with a counter variable can be used. 2. **MapReduce**: A more functional programming style approach using map and reduce methods on arrays or objects. Keep in mind that performance differences between these loop constructs may not be significant for small-scale JavaScript applications. However, when dealing with large datasets or high-performance requirements, the choice of loop construct can have a noticeable impact on execution speed.
Related benchmarks:
For Loop Approaches
For Loop Different Approaches
for loop vs every
loopstest
for loop 1234
Comments
Confirm delete:
Do you really want to delete benchmark?