String concatenation
JavaScript performance comparison
Info
Different ways to concatenate strings together
Preparation code
<script>
var arr = ['a', 'b', 'c', 'd']
// The constructor initializes an Array
StringBuilderEx = function()
{
this._buffer = new Array();
}
StringBuilderEx.prototype =
{
// This method appends the string into an array
append : function(text)
{
this._buffer[this._buffer.length] = text;
},
// This method does concatenation using JavaScript built-in function
toString : function()
{
return this._buffer.join("");
}
};
// Assigns our class to Array class
var StringBuilderEx = Array;
// Using prototype I link function append to push and toString to join
Array.prototype.append=Array.prototype.push;
Array.prototype.toString=Array.prototype.join;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Direct concatenation |
|
pending… |
Individual += statements |
|
pending… |
Individual statements |
|
pending… |
Using Array#join |
|
pending… |
Single individual statement |
|
pending… |
Function based (for) |
|
pending… |
Stringbuilder Class |
|
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:
- Revision 1: published
- Revision 2: published
- Revision 3: published
- Revision 7: published
- Revision 8: published
- Revision 10: published
- Revision 14: published by Redger
- Revision 15: published by collibra
- Revision 16: published
- Revision 17: published
- Revision 18: published
- Revision 19: published
- Revision 20: published
- Revision 21: published
- Revision 23: published
- Revision 25: published by Hans
- Revision 26: published
- Revision 27: published
- Revision 28: published
- Revision 29: published
- Revision 31: published
- Revision 32: published
- Revision 33: published
- Revision 34: published
- Revision 35: published
- Revision 36: published
- Revision 37: published
- Revision 38: published
- Revision 39: published
- Revision 40: published by ichi
- Revision 41: published
- Revision 42: published
- Revision 44: published
- Revision 45: published
- Revision 46: published
- Revision 47: published
0 comments