Javascript Filter vs loop (v3)

Revision 3 of this benchmark created on


Setup

function TestObject() {
      this.ToBeRemoved = false;
    }
    
    var myTestObjects = [];
    for (var i = 0; i < 100000; i++) {
      var myNewObject = new TestObject();
      if (i % 2) {
        myNewObject.ToBeRemoved = true;
      }
    
      myTestObjects.push(myNewObject);
    
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Filter
var Result = myTestObjects.filter(function(TestObject) {
  return !TestObject.ToBeRemoved;
});
ready
Loop
var Result = [];
var l = myTestObjects.length;
for (var i = 0; i < l; i++) {
  var TestObject = myTestObjects[i]
  if (!TestObject.ToBeRemoved) Result.push(TestObject)
}
ready

Revisions

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