aarontgrogg Array.filter vs for-loop

Benchmark created by Aaron T. Grogg on


Setup

var sidekicks = [
      { name: "Robin",     hero: "Batman"   },
      { name: "Supergirl", hero: "Superman" },
      { name: "Oracle",    hero: "Batman"   },
      { name: "Krypto",    hero: "Superman" }
  ];

Test runner

Ready to run.

Testing in
TestOps/sec
Array.filter
var batKicks = sidekicks.filter(function (el) {
    return (el.hero === "Batman");
});
ready
for loop
var batKicks = [];

for (var i = 0; i < sidekicks.length ; i++) {
    if (sidekicks[i].hero === "Batman") {
        batKicks.push(sidekicks[i]);
    }
}
ready

Revisions

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

  • Revision 1: published by Aaron T. Grogg on