filter vs forEach and push

JavaScript performance comparison

Test case created by gitlab-winnie

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const testData = [];
  for (let i = 0; i < 1000000; i += 1) {
    testData.push({ value: i, text: `some text ${i}`})
  }

};
</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
filter()
const filteredData = testData.filter(item => item.value % 3 === 0);
console.debug(filteredData);
pending…
forEach() + push()
const filteredData = []
testData.forEach(item => {
  if (item.value % 3 === 0) {
    filteredData.push(item);
  }
});
console.debug(filteredData);
pending…

Compare results of other browsers

Revisions

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

0 Comments