js-string-builders

JavaScript performance comparison

Test case created by Doncho Minkov

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function buildStringBuilder() {
      var builder = {
        _parts: [],
        append: function(value) {
          this._parts[this._parts.length] = value;
        },
        insert: function(value, index) {
          this._parts.splice(index, 0, value);
        },
        clear: function() {
          delete this._parts;
        },
        toString: function() {
          return this._parts.join("");
        }
      };
   
      return builder;
    }
   
    function StringBuilder() {
   
      var chunkChars = [];
      var length = 0;
   
      // Appends the given value to the end of this instance.
      this.append = function(value) {
        value = verify(value);
   
        var len = value.length;
   
        if (len > 0) {
          for (var i = 0; i < len; i++) {
            chunkChars[length++] = value[i];
          }
        }
   
        return this;
      };
   
      // Inserts a String in the array buffer
      this.insert = function(index, value) {
        value = verify(value);
   
        var len = value.length;
   
        if (len > 0) {
          for (var i = 0; i < len; i++) {
            chunkChars.splice(index + i, 0, value[i]);
          }
   
          length = chunkChars.length;
        }
   
        return this;
      }
   
      // Clears the array buffer.
      this.clear = function() {
        chunkChars = [];
        length = 0;
        return this;
      };
   
      // Converts this instance to a String.
      this.toString = function() {
        return chunkChars.join("");
      }
   
      var verify = function(value) {
          if (!defined(value)) return "";
          if (getType(value) != getType(new String())) return String(value);
          return value;
          };
   
      var defined = function(value) {
          return value != null && typeof(value) != "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";
          };
    }
   
    var count = 100000;
    var stringToAppend = "Sample String to append";
};
</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
StringBuilder
var sb = new StringBuilder();
for (var i = 0; i < count; i++) {
  sb.append(stringToAppend);
}

sb.toString();
pending…
buildStringBuilder
var sb = buildStringBuilder();
for (var i = 0; i < count; i++) {
  sb.append(stringToAppend);
}

sb.toString();
pending…
regular +
var sb = ""
for (var i = 0; i < count; i++) {
  sb += stringToAppend;
}

sb.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

Add a comment