Array Filter - Imperative vs Declarative (v2)

Revision 2 of this benchmark created by sem k on


Description

Compare performance of declarative object array filtering (using .filter()) with an imperative alternative (loop + .splice()).

Setup

var filteredPeople, people = [
      { id : 0, name : "Alan" },
      { id : 1, name : "Brad" },
      { id : 2, name : "Carl" },
      { id : 3, name : "Dianne" },
      { id : 4, name : "Ethan" },
      { id : 5, name : "Fergus" },
      { id : 6, name : "Carl" },
      { id : 7, name : "Harold" },
      { id : 8, name : "Isabel" },
      { id : 9, name : "Jonathan" }
    ];

Test runner

Ready to run.

Testing in
TestOps/sec
Declarative - Array.prototype.filter()
filteredPeople = people.filter(function (person) {
  return person.name !== "Carl";
});
ready
Imperative - while(), Array.prototype.splice()
var i = 0, person;
while (i < people.length) {
  person = people[i];
  if (person.name === "Carl") {
    people.splice(i, 1);
    // No increment needed due to splice
  }
  else {
    i += 1;
  }
}
filteredPeople = people;
ready

Revisions

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

  • Revision 2: published by sem k on