Function Loops (v2)

Revision 2 of this benchmark created on


Description

Testing the performance of filtering a list with a given function.

Preparation HTML

<script>
  var list = [];
  for (var i = 0; i < 500; i++) {
   list[i] = i;
  }
  
  var criteria = function(n) {
   return (n % 7 == 0) || (n % 4 == 0);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Standard For Loop
var results = [];
var item;
for (var i = 0, len = list.length; i < len; i++) {
 item = list[i];
 if (criteria(item)) results.push(item);
}
ready
Countdown Loop
var results = [];
var item;
var len = list.length;

while (--len) {
 item = list[len];
 if (criteria(item)) results.unshift(item);
}
ready
Native Filter (If supported)
var results = list.filter(criteria);
ready
Manually Inlined
var results = [];
var item;
for (var i = 0, len = list.length; i < len; i++) {
 item = list[i];
 if ((item % 7 == 0) || (item % 4 == 0)) results.push(item);
}
ready
Native Filter With Current Context(If supported)
var results = list.filter(criteria, this);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.