Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?

Does javascript use immutable or mutable strings? Do I need a "string builder"?


They are immutable. You cannot change a character within a string with something like var myString = "abbdef"; myString[2] = 'c' var myString = "abbdef"; myString[2] = 'c' . The string manipulation methods such as trim , slice return new strings.

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!).

This was what I believed would be the fastest way, though I kept thinking that adding a method call may make it slower...

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('');
}

Here are performance speed tests. All three of them create a gigantic string made up of concatenating "Hello diggity dog" one hundred thousand times into an empty string.

I've created three types of tests

  • Using Array.push and Array.join
  • Using Array indexing to avoid Array.push , then using Array.join
  • Straight string concatenation
  • Then I created the same three tests by abstracting them into StringBuilderConcat , StringBuilderArrayPush and StringBuilderArrayIndex http://jsperf.com/string-concat-without-sringbuilder/5 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 http://jsperf.com/string-concat-without-sringbuilder/5 for the old data table.

    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 ( higher is better )

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

    Findings

  • Nowadays, all browsers handle string concatenation well. Array.join only helps Opera

  • Overall, Chrome is fastest, clocking 1025 ops/sec in 27.0. 10 times faster than using Array.join()

  • Firefox is in second place at around 550 ops/sec (but 20.0 seems to have regressed). Array.join is about 4-5 times slower.

  • IE is fastest with straight string concatenation, it's really slow using Array.join and Array.push . IE 9 makes Array.join not be so slow, and all the SB abstractions perform almost the same way (probably because of the method overhead)

  • Opera is the only one where the Array.join actually helps, it's 2-3 times as fast as string concatenation.

  • 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)

  • Hope somebody else finds this useful

    Different Test Case

    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.

    I suggest everybody should keep thinking of other ways to test this, thanks for the input Roy.

    http://jsperf.com/string-concat-without-sringbuilder/7


    from the rhino book:

    In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings. Strings are assigned by reference, not by value. In general, when an object is assigned by reference, a change made to the object through one reference will be visible through all other references to the object. Because strings cannot be changed, however, you can have multiple references to a string object and not worry that the string value will change without your knowing it


    Performance tip:

    If you have to concatenate large strings, put the string parts into an array and use the Array.Join() method to get the overall string. This can be many times faster for concatenating a large number of strings.

    There is no StringBuilder in JavaScript.

    链接地址: http://www.djcxy.com/p/5154.html

    上一篇: JavaScript开发人员是否有理由不使用Array.push()?

    下一篇: JavaScript字符串是不可变的吗? 我需要JavaScript中的“字符串生成器”吗?