Array filter performance (v14)

Revision 14 of this benchmark created by Javier Lazaro on


Setup

var alpha = [395,608,163,584,921,499,469,404,520,534,556,133,488,977,506,205,62,918,770,879,342,90,108,770,605,599,436,942,609,967,602,283,503,920,401,747,298,541,2,291,681,383,725,537,839,86,886,574,753,924,916,158,331,741,277,685,748,664,496,247,613,884,243,294,118,956,202,114,514,232,284,754,457,89,35,896,565,115,497,987,176,17,388,793,487,691,596,580,644,566,381,783,268,585,36,292,765,250,895,59,989,89,674,808,342,724,373,791,447,258,776,120,790,243,115,120,38,372,245,494,326,209,736,328,312,779,900,642,389,975,153,210,533,115,537,958,392,222,380,891,322,633,818,518,220,476,548,812,474,436,676,26,294,385,893,984,320,363,510,788,595,368,126,292,40,570,23,480,467,556,85,357,59,300,28,897,754,3,195,206,146,967,49,399,930,905,975,933,956,266,742,613,598,564,783,992,844,764,176,186,536,729,26,501,110,73,211,173,219,843,280,604,30,786,954,112,870,460,194,103,788,626,121,545,265,177,535,420,947,636,103,554,458,71,138,528,842,704,4,526,740,468,935,244,691,662,22,152,783,647,205,130,462,841,264,857,108,888,500,967,984,840,200,931,21,680,579,343,237,787,347,317,257,423,154,295,909,541,385,992,955,553,976,572,416,753,861,931,339,339,732,149,870,749,397,127,327,499,861,62,53,900,381,482,535,275,525,558,378,429,391,96,766,954,895,794,722,943,245,174,236,721,107,381,328,462,30,74,717,857,735,846,891,894,594,784,221,49,662,891,843,140,366,993,245,389,653,721,70,300,157,285,808,253,701,160,883,849,533,52,715,478,866,848,828,226,457,510,410,418,166,267,973,411,708,396,11,362,809,282,735,321,302,99,688,136,713,581,945,88,240,586,970,226,507,379,672,795,717,404,898,175,427,213,567,826,113,939,138,703,346,170,183,644,340,999,748,789,362,316,52,453,303,289,372,372,60,390,205,665,431,305,318,417,891,140,293,580,771,24,478,115,618,628,339,746,908,246,566,750,186,849,848,44,825,74,5,40,528,681,584,567,93,136,499,288,650,649,155,752,750,689,240,619,546,884,388,332,843,705,68,759,361,251,541,625,54,594,810,875,320,188,909,434,465,739,635,26,446,980,835,455,143,599,308,899,867,319,274,572,499,718,560,333,620,271,929,351,888,96,894,469,380,997,676,443,233,897,454,85,68,337,910,156,726,587,690,991,541,801,853,485,557,262,810,80,550,64,39,777,383,703,578,572,431,375,763,219,276,561,497,795,601,390,634,181,325,478,679,620,580,123,39,533,172,617,30,165,756,593,987,559,72,212,55,26,380,941,470,814,488,772,727,392,303,993,74,302,175,395,962,838,378,266,437,656,796,152,479,197,672,736,390,942,143,523,159,134,69,868,933,599,691,252,939,303,491,971,900,974,288,908,858,210,710,346,960,355,882,876,349,358,313,796,443,314,932,407,87,331,837,993,30,10,464,545,543,233,185,860,390,256,580,560,737,578,946,682,448,236,991,415,400,126,955,263,736,967,675,177,775,108,519,770,749,927,661,387,771,442,857,514,112,286,871,104,840,483,537,262,987,140,484,4,782,954,140,906,310,905,869,616,908,277,857,506,316,427,193,412,75,196,339,933,277,758,210,664,914,299,586,101,870,25,945,163,370,677,960,670,884,471,624,295,694,865,179,681,438,193,493,102,353,953,916,262,669,960,917,503,952,931,769,711,473,198,879,808,429,443,547,967,287,359,987,925,76,146,585,348,451,71,136,627,947,549,855,498,510,239,148,416,787,185,294,54,319,96,842,814,208,258,776,90,333,739,175,978,494,723,994,757,852,850,436,291,523,229,906,345,891,283,238,961,776,201,999,854,106,573,460,179,405,380,556,745,8,70,986,241,729,356,450,459,415,944,111,658,953,194,924,232,90,66,158,161,759,271,179,293,130,304,33,709,905,441,693,61,65,825,225,160,866,480,259,619,436,352,625,760,119,767,382,0,672,276,167,5,586,9,852,526,775,830,858,92,826,433,798,906,782,983,463,682,217,795,62,380,278,689,358,630,543,621,253,351,172,952,608,790,505,126,965,306,264,574,70,47,113,856,106,247,187,832,815,664,746,623,508,911,30,507,708,278,97,713,195,307,440,816,477,433,875,408,983,80,858,470,686,665,766,641,102,384,899,802,894,896,957,417,493,952,62,49,176,198,56,674,405,258,474,579,554,649,541,505,979,940,417,949,737,840,102,748,271,927,37,395,980,687];
    
    
    var testElement = function(i) {
      return i > 500;
    }
    
    Array.prototype.filter2 = function(fun /*, thisArg */ ) {
      "use strict";
    
      if (this === void 0 || this === null)
        throw new TypeError();
    
      var t = Object(this);
      var len = t.length >>> 0;
      if (typeof fun !== "function")
        throw new TypeError();
    
      var res = [];
      var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
      for (var i = 0; i < len; i++) {
        if (i in t) {
          var val = t[i];
    
          // NOTE: Technically this should Object.defineProperty at
          //       the next index, as push can be affected by
          //       properties on Object.prototype and Array.prototype.
          //       But that method's new, and collisions should be
          //       rare, so use the more-compatible alternative.
          if (fun.call(thisArg, val, i, t))
            res.push(val);
        }
      }
    
      return res;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Filter
var result = alpha.filter(testElement);
ready
Loop
function filter(a) {
  b = a.slice(0);
  for (var i = a.length - 1; i >= 0; i--) {
    if (!testElement(a[i]) !== true) b.splice(i, 1)
  }
  return b
}

var result = filter(alpha);
ready
bla
function filter(a) {
  b = a.slice(0);
  for (var i = a.length - 1; i >= 0; i--) {
    if (!testElement(a[i])) b.splice(i, 1)
  }
  return b
}

var result = filter(alpha);
ready
blubb
var result = alpha.filter2(testElement);
ready

Revisions

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