Equally sort array pair

JavaScript performance comparison

Revision 3 of this test case created by C5H8NNaO4

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a = [];
  var b = [];
  var obj;
  var arr;
  var max = 10000;
  
  for (var i = 0; i < max; i++) {
    if (i % 5) {
      arr = [1, 2];
      b[max - i - 1] = arr;
      a[i] = arr;
    } else if (i % 2) {
      obj = {
        "hello": "world"
      };
      b[max - i - 1] = obj;
      a[i] = obj;
    } else {
      b[max - i - 1] = i % 100;
      a[i] = i % 100;
    }
  }
  
  var sort = function (el) {
      var i = 0,ret;
      !sort.el && (sort.el = el);
      ret = el.sort (srt);
      if (sort.el != el) {
        var l = Math.max(el.length,sort.el.length);
        for (var j = 0;j<l;j++) {
            if (typeof el[j] === "object" && el[j] && el[j]._srt) {}
            delete el[j]._srt;
              if (typeof sort.el[j] === "object" && sort.el[j] && sort.el[j]._srt) {}
            delete sort.el[j]._srt;
        }
      }
      sort.el = el
      return ret;
      function srt (a,b,c) {
        var types = [typeof a,typeof b];
        var r = [0,0];
        
        if (types[0] === "object" && types[1] ===  "object") {
  
          if (el === sort.el) {
            !a._srt && (a._srt = ++i);
            !b._srt && (b._srt = ++i);
            r[0] = a._srt;
            r[1] = b._srt;         
          } else {
            a._srt && (r[0] = a._srt) 
            b._srt && (r[1] = b._srt) 
          }
          return r[0] - r[1];
        }
        if (types[0] == "object") {
          return 1;
        }
        if (types[1] == "object") {
          return -1;
        }
        return a>b?1:a<b?-1:0;   
      }
    }
  
  var sort2 = function sort (curr,prev) {
      if (prev) { //mark the objects
          for (var i = prev.length,j,t;i>0;i--) {
              t = typeof (j = prev[i]);
              if (j != null && t === "object") {
                   j._pos = i;   
              } else if (t !== "object" && t != "undefined" ) break;
          }
      }
  
      curr.sort (sorter);
  
      if (prev) {
          for (var k = prev.length,l,t;k>0;k--) {
              t = typeof (l = prev[k]);
              if (t === "object" && l != null) {
                  delete l._pos;
              } else if (t !== "object" && t != "undefined" ) break;
          }
      }
      return curr;
      function sorter (a,b) {
  
           var tStr = Object.prototype.toString
           var types = [tStr.call(a),tStr.call(b)]
           var ret = [0,0];
           if (types[0] === types[1] && types[0] === "[object Object]") {
               if (prev) return a._pos - b._pos
               else {
                   return a === b ? 0 : 1;
               }
           } 
           if (a == null) return 1;
           if (b == null) return -1;
           if (types[0] === "[object Object]")
               return 1;
           if (types[1] === "[object Object]")
               return -1;
  
  
          return a>b?1:a<b?-1:0;
      }
  
  }
  
  var sortPumba = (function () {
      var tmp;
  
      function sorter(a, b) {
          var types = [typeof a, typeof b];
          if (types[0] === "object" && types[1] === "object") {
              return tmp.indexOf(a) - tmp.indexOf(b); // sort by position in original
          }
          if (types[0] == "object") {
              return 1;
          }
          if (types[1] == "object") {
              return -1;
          }
          return a > b ? 1 : a < b ? -1 : 0;
      }
  
  
      return function (el) {
          if (tmp) {
              for (var i = 0; i < el.length; i++) {
                  el[i] = tmp[i]; // edit el in-place, same order as tmp
              }
              return el;
          }
          tmp = [].slice.call(el); // copy original
          return tmp = el.sort(sorter); // cache result
      };
  
  })();
  
  
  function sort3 (curr,prev) {
           var weight = {
              "[object Undefined]":6,         
              "[object Object]":5,
              "[object Null]":4,
              "[object String]":3,
              "[object Number]":2,
              "[object Boolean]":1
          }
          if (prev) { //mark the objects
              for (var i = prev.length,j,t;i>0;i--) {
                  t = typeof (j = prev[i]);
                  if (j != null && t === "object") {
                       j._pos = i;   
                  } else if (t !== "object" && t != "undefined" ) break;
              }
          }
      
          curr.sort (sorter);
          
          if (prev) {
              for (var k = prev.length,l,t;k>0;k--) {
                  t = typeof (l = prev[k]);
                  if (t === "object" && l != null) {
                      delete l._pos;
                  } else if (t !== "object" && t != "undefined" ) break;
              }
          }
          return curr;
      
          function sorter (a,b) {
      
               var tStr = Object.prototype.toString
               var types = [tStr.call(a),tStr.call(b)]
               var ret = [0,0];
               if (types[0] === types[1] && types[0] === "[object Object]") {
                   if (prev) return a._pos - b._pos
                   else {
                       return a === b ? 0 : 1;
                   }
               } else if (types [0] !== types [1]){
                       return weight[types[0]] - weight[types[1]]
               }
               
      
              
              return a>b?1:a<b?-1:0;
          }
      
      }

};
</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
Sort 1
sort (a);
sort (b);
pending…
Sort 2
sort2 (a);
sort2 (b,a);
pending…
Sort 3
sort3 (a);
sort3 (b,a);
pending…
Pumbaa80
sortPumba (a);
sortPumba (b);
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