Array.filterMap

JavaScript performance comparison

Revision 2 of this test case created by Akash Kava

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var customers = [
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: true, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" },
      { active: false, name: "a", email: "b" }
  ];
  
  Array.prototype.filterMap = function(filter) {
     const r = [];
     const len = this.length;
     for(let i=0; i<len; i++) {
        const item = filter(this[i], i, this);
        if (item !== undefined) {
           r.push(item);
        }
     }
     return r;
  };

};
</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
filterMap
var emails = customers.filterMap(
   (c) => c.active ?
      { name: c.name, email: c.email } :
      undefined);
pending…
filter and map
var emails = customers.filter( (c) => c.active)
            .map( (c) => ({ name: c.name, email: c.email }));
pending…
reduce
const emails = customers.reduce(
  (prev, c) => c.active
    ? [ ...prev, { name: c.name, email: c.email } ]
    : prev,
  []
)
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