Concatenar Strings

JavaScript performance comparison

Test case created by mgibsonbr

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  (function(){
      function create() {
          function StringBuilder(){
              if (!(this instanceof StringBuilder)) {
                  return new StringBuilder();
              }
  
              this.values = [];
              this.length = 0;
          }
  
          StringBuilder.prototype.append = function(value) {
              this.values.push(value);
              this.length += value.length;
          };
          StringBuilder.prototype.valueOf = function() {
              return this.toString();
          };
          StringBuilder.prototype.toString = function() {
              return this.values.join("");
          };
  
          return StringBuilder;
      }
  
      if (typeof define === "function" && define.amd) {
          define(function(){
              return create();
          });
      } else {
          window.StringBuilder = create();
      }
  }());

};
</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
join (tamanho fixo)
var arr = new Array(1000);
for (var i = 0; i < 1000; i++) {
    arr.push(i.toString());
}
var myString = arr.join('');
pending…
StringBuilder
var myStringBuilder = StringBuilder();
for (var i = 0; i < 1000; i++) {
    myStringBuilder.append(i.toString());
}
var myString = myStringBuilder.toString();
pending…
Soma strings
var myString = "";
for (var i = 0; i < 1000; i++) {
    myString += i.toString();
}
pending…
join
var arr = [];
for (var i = 0; i < 1000; i++) {
    arr.push(i.toString());
}
var myString = arr.join('');
pending…
Uint16Array
var arr = new Uint16Array(10000);
var indice = 0;
for (var i = 0; i < 1000; i++) {
    var str = i.toString();
    for ( var t = 0 ; t < str.length ; t++ )
        arr[indice++] = str.charCodeAt(t);
}
var myString = String.fromCharCode.apply(null, arr);
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