Collation String Sorting

JavaScript performance comparison

Revision 14 of this test case created by Bergi

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var sortbyalphabet_timo_closure = function() {
      // PREPARATION STARTS
      var i, alphabet = "-0123456789AaÀàÁáÂâÃãÄäBbCcÇçDdÈèÉéÊêËëFfGgHhÌìÍíÎîÏïJjKkLlMmNnÑñOoÒòÓóÔôÕõÖöPpQqRrSsTtUuÙùÚúÛûÜüVvWwXxÝýŸÿZz",
      index = {};
   
      i = alphabet.length;
      while (i--) index[alphabet.charCodeAt(i)] = i;
      // PREPARATION ENDS
   
      return function(a, b) {
        var i, len, diff;
   
        if (typeof a === "string" && typeof b === "string") {
          (a.length > b.length) ? len = a.length : len = b.length;
          for (i = 0; i < len; i++) {
            diff = index[a.charCodeAt(i)] - index[b.charCodeAt(i)];
   
            if (diff !== 0) {
              return diff;
            }
          }
          // sort the shorter first
          return a.length - b.length;
        } else {
          return 0;
        }
      };
    };
   
    var sortbyalphabet_timo = sortbyalphabet_timo_closure();
   
    var words = ['tauschen', 'täuschen', 'andern', 'ändern', 'Ast', 'Äste', 'dosen', 'dösen', 'Donaudamfschiffahrtskapitänskajütenschild-0', 'Donaudamfschiffahrtskapitänskajütenschild-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
Closure
var sorted = words.sort(sortbyalphabet_timo);
pending…
Non-closure
var sorted = words.sort(sortbyalphabet_timo_closure());
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. Here’s a list of current revisions for this page:

0 comments

Add a comment