String concat without SringBuilder

JavaScript performance comparison

Test case created

Preparation code

<script type="text/javascript">
function StringBuilderConcat() {
  this._str = "";
}

StringBuilderConcat.prototype.append = function (str) {
    this._str += str;
}

StringBuilderConcat.prototype.toString = function () {
  return this._str;
}

function StringBuilderArrayPush() {
    this._array = [];
}

StringBuilderArrayPush.prototype.append = function (str) {
    this._array.push(str);
}

StringBuilderArrayPush.prototype.toString = function () {
  return this._array.join('');
}

function StringBuilderArrayIndex() {
  this._array = [];
  this._index = 0;
}

StringBuilderArrayIndex.prototype.append = function (str) {
    this._array[this._index] = str;
    this._index++;
}

StringBuilderArrayIndex.prototype.toString = function () {
  return this._array.join('');
}
</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
Array index - No Abstraction
var build = [];
for (var i=0; i < 1000000; i ++) {
  build[i] = "Hello diggity dog";
}
var out = build.join('');
pending…
Array push - No abstraction
var build = [];
for (var i=0; i < 1000000; i ++) {
  build.push("Hello diggity dog");
}

var out = build.join('');
pending…
Straight String Concat - No abstraction
var build = "";
for (var i=0; i < 1000000; i ++) {
  build += "Hello diggity dog";
}
 
pending…
StringBuilder - Array index
var start = new Date();
var builder = new StringBuilderArrayIndex();
for (var i=0; i < 1000000; i ++) {
  builder.append("Hello diggity dog");
}
var out = builder.toString();
pending…
StringBuilder - Array push
var builder = new StringBuilderArrayPush();
for (var i=0; i < 1000000; i ++) {
  builder.append("Hello diggity dog");
}
var out = builder.toString();
pending…
StringBuilder - string concat
var builder = new StringBuilderConcat();
for (var i=0; i < 1000000; i ++) {
  builder.append("Hello diggity dog");
}
var out = builder.toString();
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. Here’s a list of current revisions for this page:

0 comments

Add a comment