string building

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var str = '';
    var array;
   
    // generate a random 20 character string
    (function () {
      var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz",
          rnum;
   
      for (var i = 0; i < 20; i++) {
        rnum = Math.floor(Math.random() * 61);
        str += chars.substring(rnum, rnum + 1);
      }
      array = str.split('');
    }());
};
</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
concatenate
var s = '';
for(var i = 0, len = array.length; i < len; i++) {
    s = s + array[i];
}
pending…
push
var a = [], s = '';
for(var i = 0, len = array.length; i < len; i++) {
   a.push(array[i]);
}
s = a.join('');
pending…
concatenate 2
var s = '';
for(var i = 0, len = array.length; i < len; i++) {
    s += array[i];
}
pending…
concatenate 2 re-evaluate length
var s = '';
for(var i = 0; i < array.length; i++) {
    s += array[i];
}
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