in place filter

Benchmark created by my name on


Setup

var A = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7];

Test runner

Ready to run.

Testing in
TestOps/sec
splice by 1
for (var i = A.length; i >= 0; i--) {
  if (A[i] === 0) {
    A.splice(i, 1);
  }
}
ready
splice by chunk
var lastSpliceIndex = -1;

for (var i = A.length; i >= 0; i--) {
  var A_VALUE = A[i];

  if (A_VALUE === 0 && lastSpliceIndex === -1) {
    lastSpliceIndex = i;
  } else if (A_VALUE !== 0 && lastSpliceIndex !== -1) {
    A.splice(i + 1, lastSpliceIndex - i);

    lastSpliceIndex = -1;
  }
}

if (lastSpliceIndex !== -1) {
  A.splice(lastSpliceIndex + 1);
}
ready

Revisions

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

  • Revision 1: published by my name on