array loop func vs filter (v15)

Revision 15 of this benchmark created by Samuel on


Preparation HTML

Filter: <span id="filter"></span>
Loop: <span id="loop"></span>

<script>
  var i = 1000;
  var data = new Array(i);
  for (;i--;) {
    data[i] = Math.ceil(Math.random()*1000);
  }

  var out;

  function loopFunc (el) {
    var i = 0,
        j = el.length,
        res = [];
  
    for (;i !== j; i++) {
      if (el[i] > 500) res.push(el[i])
    }

    return res;
  };

  var elFilter = document.getElementById('filter');
  var elLoop = document.getElementById('loop');
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
filter
elFilter.innerHTML = data.filter(function (el) {
  return el > 500;
}).length;
ready
loop
elLoop.innerHTML = loopFunc(data).length;
ready

Revisions

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