Strings concatenation

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function StringBuilder()
  {
      var strings = [];
  
      this.append = function (string)
      {
          string = verify(string);
          if (string.length > 0) strings[strings.length] = string;
      };
  
      this.appendLine = function (string)
      {
          string = verify(string);
          if (this.isEmpty())
          {
              if (string.length > 0) strings[strings.length] = string;
              else return;
          }
          else strings[strings.length] = string.length > 0 ? "\r\n" + string : "\r\n";
      };
  
      this.clear = function () { strings = []; };
  
      this.isEmpty = function () { return strings.length === 0; };
  
      this.toString = function () { return strings.join(""); };
  
      var verify = function (string)
      {
          if (!defined(string)) return "";
          if (getType(string) != getType(new String())) return String(string);
          return string;
      };
  
      var defined = function (el)
      {
          // Changed per Ryan O'Hara's comment:
          return el !== null && typeof(el) != "undefined";
      };
  
      var getType = function (instance)
      {
          if (!defined(instance.constructor)) throw Error("Unexpected object type");
          var type = String(instance.constructor).match(/function\s+(\w+)/);
  
          return defined(type) ? type[1] : "undefined";
      };
  }
  
  StringBuilder.formatDate = function(dateMillis) {
      var dateText = new StringBuilder();
      var date = new Date(dateMillis);
  
      dateText.append(date.getDate());
      dateText.append('/');
      dateText.append(date.getMonth()+1);
      dateText.append('/');
      dateText.append(date.getFullYear());
      dateText.append(' ');
      dateText.append(date.getHours());
      dateText.append(':');
      dateText.append(date.getMinutes());
      
      return dateText.toString();
  }

};
</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
Native concat() method
var str1 = 'myString';
var result = str1.concat(' str');
pending…
Simple + concatenation
var str1 = 'myString';
var result = str1 + ' str';
pending…
StringBuilder concatenation
var str1 = new StringBuilder();
str1.append(' str');
result = str1.toString();
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