Collation String Sorting

JavaScript performance comparison

Test case created by Tomalak

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function sortbyalphabet_jakov(a, b) {
      var alphabet = "0123456789AaÀàÁáÂâÃãÄäBbCcÇçDdÈèÉéÊêËëFfGgHhÌìÍíÎîÏïJjKkLlMmNnÑñOoÒòÓóÔôÕõÖöPpQqRrSsTtÙùÚúÛûÜüVvWwXxÝýŸÿZz";
      var a = a.toLowerCase();
      var b = b.toLowerCase();
      var shorterone = (a.length > b.length ? a : b);
      for (i = 0; i < shorterone.length; i++) {
        var diff = alphabet.indexOf(a.charAt(i)) - alphabet.indexOf(b.charAt(i));
        if (diff != 0) {
          return diff;
        }
      }
      // sort the shorter first
      return a.length - b.length;
    }
   
    var sortbyalphabet_tomalak = (function() {
      var i, alphabet = "0123456789AaÀàÁáÂâÃãÄäBbCcÇçDdÈèÉéÊêËëFfGgHhÌìÍíÎîÏïJjKkLlMmNnÑñOoÒòÓóÔôÕõÖöPpQqRrSsTtÙùÚúÛûÜüVvWwXxÝýŸÿZz",
          index = {};
   
      for (i = 0; i < alphabet.length; i++) {
        index[alphabet.charAt(i)] = i;
      }
   
      return function(a, b) {
        var i, len, diff;
   
        if (typeof a === "string" && typeof b === "string") {
          len = Math.max(a.length, b.length);
          for (i = 0; i < len; i++) {
            diff = index[a.charAt(i)] - index[b.charAt(i)];
            if (diff !== 0) {
              return diff;
            }
          }
          // sort the shorter first
          return a.length - b.length;
        } else {
          return 0;
        }
      };
    })();
   
    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
jakov
var sorted = words.sort(sortbyalphabet_jakov);
pending…
Tomalak
var sorted = words.sort(sortbyalphabet_tomalak);
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