Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They are immutable. You cannot change a character within a string with something like <code>var myString = "abbdef"; myString[2] = 'c'</code>. The string manipulation methods such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim" rel="noreferrer"><code>trim</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice" rel="noreferrer"><code>slice</code></a> return new strings.</p> <p>However, I've always heard what Ash mentioned in his answer (that using Array.join is faster for concatenation) so I wanted to test out the different methods of concatenating strings and abstracting the fastest way into a StringBuilder. I wrote some tests to see if this is true (it isn't!).</p> <p>This was what I believed would be the fastest way, though I kept thinking that adding a method call may make it slower...</p> <pre><code>function StringBuilder() { this._array = []; this._index = 0; } StringBuilder.prototype.append = function (str) { this._array[this._index] = str; this._index++; } StringBuilder.prototype.toString = function () { return this._array.join(''); } </code></pre> <p>Here are performance speed tests. All three of them create a gigantic string made up of concatenating <code>"Hello diggity dog"</code> one hundred thousand times into an empty string.</p> <p>I've created three types of tests</p> <ul> <li>Using <code>Array.push</code> and <code>Array.join</code></li> <li>Using Array indexing to avoid <code>Array.push</code>, then using <code>Array.join</code></li> <li>Straight string concatenation</li> </ul> <p>Then I created the same three tests by abstracting them into <code>StringBuilderConcat</code>, <code>StringBuilderArrayPush</code> and <code>StringBuilderArrayIndex</code> <a href="http://jsperf.com/string-concat-without-sringbuilder/5" rel="noreferrer">http://jsperf.com/string-concat-without-sringbuilder/5</a> Please go there and run tests so we can get a nice sample. Note that I fixed a small bug, so the data for the tests got wiped, I will update the table once there's enough performance data. Go to <a href="http://jsperf.com/string-concat-without-sringbuilder/5" rel="noreferrer">http://jsperf.com/string-concat-without-sringbuilder/5</a> for the old data table.</p> <p>Here are some numbers from Feb 21, 2013, if you don't want to follow the link. The number on each test is in operations/second (<strong>higher is better</strong>)</p> <pre><code>| Browser | Index | Push | Concat | SBIndex | SBPush | SBConcat | --------------------------------------------------------------------------- | Chrome 24.0.1312 | 83 | 87 | 702 | 69 | 87 | 165 | | Chrome 25.0.1364 | 43 | 47 | 620 | 42 | 42 | 68 | | Firefox 10.0.10 | 164 | 164 | 533 | 164 | 16 | 421 | | Firefox 19.0 | 70 | 70 | 259 | 70 | 70 | 186 | | Exploder 7.0 | 51 | 33 | 58 | 31 | 37 | 45 | | Exploder 8.0 | 48 | 30 | 58 | 30 | 36 | 36 | | Exploder 9.0 | 87 | 64 | 95 | 61 | 61 | 61 | | Opera 12.14 | 125 | 154 | 66 | 106 | 137 | 63 | </code></pre> <p><strong>Findings</strong></p> <ul> <li><p>Nowadays, all browsers handle string concatenation well. <code>Array.join</code> only helps Opera</p></li> <li><p>Overall, Chrome is fastest, clocking 1025 ops/sec in 27.0. 10 times faster than using Array.join()</p></li> <li><p>Firefox is in second place at around 550 ops/sec (but 20.0 seems to have regressed). <code>Array.join</code> is about 4-5 times slower.</p></li> <li><p>IE is fastest with straight string concatenation, it's really slow using <code>Array.join</code> and <code>Array.push</code>. IE 9 makes <code>Array.join</code> not be so slow, and all the SB abstractions perform almost the same way (probably because of the method overhead)</p></li> <li><p>Opera is the only one where the <code>Array.join</code> actually helps, it's 2-3 times as fast as string concatenation.</p></li> <li><p>Creating a StringBuilder to abstract away each browser's performance issues does more harm than good. The overhead of method calls may be acceptable but the tendency seems to be that browsers are handling string concatenation more smartly. It would only make sense if your target audience is Opera, so you can use Array.join there and use String concatenation everywhere else (this means all the other browsers are taking a hit)</p></li> </ul> <p>Hope somebody else finds this useful</p> <p><strong>Different Test Case</strong></p> <p>Since @RoyTinker thought that my test was flawed, I created a new case that doesn't create a big string by concatenating the same string, it uses a different character for each iteration. String concatenation still seemed faster or just as fast. Let's get those tests running. </p> <p>I suggest everybody should keep thinking of other ways to test this, thanks for the input Roy.</p> <p><a href="http://jsperf.com/string-concat-without-sringbuilder/7" rel="noreferrer">http://jsperf.com/string-concat-without-sringbuilder/7</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload