arr-map-filter-reduce

JavaScript performance comparison

Revision 3 of this test case created by G. Lathoud

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var n = 10000,
    arr = new Array(n)
    drop = 0.1 // Proportion of numbers to drop
    ;
  
  // Deterministic pseudo-random numbers to make sure
  // arr is always generated the same way.
  // http://stackoverflow.com/questions/521295/javascript-random-seeds
  var seed = 1;
  
  function random() {
    var x = Math.sin(seed++) * 10000;
    return x - Math.floor(x);
  }
  
  for (var i = n; i--;)
    arr[i] = {
      p: random() < drop ? null : i
    };
  
    function* map_p( data )
    {
        for (var x of data)
            yield x.p;
    }
    
    function* filter_null( data )
    {
        for (var x of data)
            if (x != null)
                yield x;
    }
    
    function sum_of( data )
    {
        var ret = 0;
        for (var x of data)
            ret += x;
    
        return ret;
    }
    
  

};
</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
3 loops
// .map(".p")
var tmp_1 = new Array(n);
for (var i = 0; i < n; i++)
  tmp_1[i] = arr[i].p;

// .filter("!=null")
var tmp_2 = [];
for (var i = 0; i < n; i++) {
  var v = tmp_1[i];
  if (v != null)
    tmp_2.push(v);
}

// .reduce("+")
var sum = 0;
for (var n2 = tmp_2.length, i = 0; i < n2; i++)
  sum = sum + tmp_2[i];
pending…
1 loop
  var sum = 0,
    n = arr.length;
  for (var i = 0; i < n; i++) {
    var v = arr[i];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")
  }
pending…
1 loop (duff)
  var sum = 0,
    i = arr.length,
    v;
  for (i; i > 7;) {

    v = arr[i - 1];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i - 2];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i - 3];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i - 4];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i - 5];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i - 6];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i - 7];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

    v = arr[i -= 8];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")
  }

  for (i; i > 0;) {

    v = arr[--i];
    v = v.p; // .map(".p")
    if (v != null) // .filter("!=null")
      sum = sum + v; // .reduce("+")

  }
pending…
3 generators
sum = sum_of( filter_null( map_p( arr )));
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