for loop vs filter api

JavaScript performance comparison

Test case created by liuliangsir

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = Array.from({length: 10000}, () => Math.floor(Math.random() * 40000));

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for loop
function siblingUnique(arr) {
 if (!Array.isArray(arr)) return [];
 if (arr.length < 2) return arr;

 const result = [];
 for (
  let i = 0, j = 0, item = 0, length = arr.length;
  i < length;
  i++
  ) {
  result.push (
   (j = i + 1, item = arr [i])
  );

  while (item === arr [j] && j < length) j++;
  if (j > i + 1) i = j - 1;
 }

 return result;
}

siblingUnique(arr);
pending…
filter
function siblingUnique(arr) {
  let last;
  return arr.filter (val => {
    let isDiff = last !== val;
    last = val;
    return isDiff;
  });
}
siblingUnique(arr);
pending…

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

Compare results of other browsers

0 Comments