locale string fuckery

JavaScript performance comparison

Test case created by marcgfx

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var config = {useGrouping:true}, iso = "en";
  var localeStringRef = (1000000000000000).toLocaleString(iso,config);
  var charArray = localeStringRef.split('');
  
  function formatAlt(n){
          var pos = charArray.length-1;
          while(n && pos>=0){
              var s = localeStringRef[pos];
              if(s==='0'){
                  var d = n % 10;
                  n = ~~(n / 10);
                  charArray[pos] = d;
              }else {
                  charArray[pos]=s;
              }
              pos--;
          }
          return charArray.join('').substr(pos+1);
  }
  
  function format(n){
    return (n).toLocaleString(iso,config);
  }

};
</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
toLocaleString
var out = '';
for(var i=1000000;i<2000000;i+=232342){
out += format(i);
}
pending…
alternative
var out = '';
for(var i=1000000;i<2000000;i+=232342){
out += formatAlt(i);
}
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