sort-compare

JavaScript performance comparison

Test case created by Kyle Simpson

Info

Examining various algorithms for sorting non-strings.

Preparation code

<script>
  function sort_1(el_1, el_2) {
   if (el_1.name < el_2.name) return -1;
   else if (el_1.name > el_2.name) return 1;
   else return 0;
  }
 
  function sort_2(names) {
   var orig_names = [].slice.call(names),
       names_only = [],
       i;
   for (i = 0; i < orig_names.length; i++) {
    names_only.push(orig_names[i].name + ":" + i);
   }
   names_only.sort();
   for (i = 0; i < names_only.length; i++) {
    names[i] = orig_names[~~ ((names_only[i].split(":"))[1])];
   }
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
native sort with callback
var names = [{
 name: "Fred",
 occupation: "carpenter"
},
{
 name: "Bill",
 occupation: "plumber"
},
{
 name: "Henry",
 occupation: "developer"
}];

names.sort(sort_1);
pending…
native sort without callback
var names = [{
 name: "Fred",
 occupation: "carpenter"
},
{
 name: "Bill",
 occupation: "plumber"
},
{
 name: "Henry",
 occupation: "developer"
}];

sort_2(names);
pending…

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

Compare results of other browsers

0 comments

Add a comment