sort objects by numeric property (v3)

Revision 3 of this benchmark created by Paul Grenier on


Preparation HTML

<script>
  var arr = [];
  var fill = function() {
   var i, len;
   for (i = 0, len = 100; i < len; i += 1) {
    arr.unshift({
     num: i
    });
   }
  };
  fill();
  var arr2 = [];
  var fill2 = function() {
   var i, len;
   for (i = 0, len = 100; i < len; i += 1) {
    arr2.unshift({
     num: "" + i
    });
   }
  };
  fill2();
  replaceString = function() {
   return this.num;
  };
  var sortFn = function(a, b) {
   return a.num - b.num
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
one
var test = arr.slice();
test.sort(sortFn);
ready
two
var test = arr2.slice();
test.sort(sortFn);
ready
three
var test = arr.slice();
var save = Object.prototype.toString;
Object.prototype.toString = replaceString;
test.sort(sortFn);
Object.prototype.toString = save;
ready
four
var test = arr.slice();
var save = Object.prototype.toString;
Object.prototype.toString = replaceString;
test.sort();
Object.prototype.toString = save;
ready
five
var test = arr2.slice();
var save = Object.prototype.toString;
Object.prototype.toString = replaceString;
test.sort();
Object.prototype.toString = save;
ready
six
var test = arr2.slice();
var save = Object.prototype.toString;
Object.prototype.toString = replaceString;
test.sort(sortFn);
Object.prototype.toString = save;
ready
seven
var test = arr2.slice();
test.sort(sortFn);
ready

Revisions

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

  • Revision 3: published by Paul Grenier on