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

后端 未结 10 2463
挽巷
挽巷 2020-11-22 02:48

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

相关标签:
10条回答
  • 2020-11-22 03:41

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

    In the same way, if you have two references to the same string, modifying one doesn't affect the other

    let a = b = "hello";
    a = a + " world";
    // b is not affected
    

    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 (Latest update in Ma5rch 2018), if you don't want to follow the link. The number on each test is in 1000 operations/second (higher is better)

    | Browser          | Index | Push | Concat | SBIndex | SBPush | SBConcat |
    ---------------------------------------------------------------------------
    | Chrome 71.0.3578 | 988   | 1006 | 2902   | 963     | 1008   | 2902     |
    | Firefox 65       | 1979  | 1902 | 2197   | 1917    | 1873   | 1953     |
    | Edge             | 593   | 373  | 952    | 361     | 415    | 444      |
    | Exploder 11      | 655   | 532  | 761    | 537     | 567    | 387      |
    | Opera 58.0.3135  | 1135  | 1200 | 4357   | 1137    | 1188   | 4294     | 
    

    Findings

    • Nowadays, all evergreen browsers handle string concatenation well. Array.join only helps IE 11

    • Overall, Opera is fastest, 4 times as fast as Array.join

    • Firefox is second and Array.join is only slightly slower in FF but considerably slower (3x) in Chrome.

    • Chrome is third but string concat is 3 times faster than Array.join

    • Creating a StringBuilder seems to not affect perfomance too much.

    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, and feel free to add new links to different test cases below.

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

    0 讨论(0)
  • 2020-11-22 03:48

    The string type value is immutable, but the String object, which is created by using the String() constructor, is mutable, because it is an object and you can add new properties to it.

    > var str = new String("test")
    undefined
    > str
    [String: 'test']
    > str.newProp = "some value"
    'some value'
    > str
    { [String: 'test'] newProp: 'some value' }
    

    Meanwhile, although you can add new properties, you can't change the already existing properties

    A screenshot of a test in Chrome console

    In conclusion, 1. all string type value (primitive type) is immutable. 2. The String object is mutable, but the string type value (primitive type) it contains is immutable.

    0 讨论(0)
  • 2020-11-22 03:50

    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.

    0 讨论(0)
  • 2020-11-22 03:50

    Just to clarify for simple minds like mine (from MDN):

    Immutables are the objects whose state cannot be changed once the object is created.

    String and Numbers are Immutable.

    Immutable means that:

    You can make a variable name point to a new value, but the previous value is still held in memory. Hence the need for garbage collection.

    var immutableString = "Hello";

    // In the above code, a new object with string value is created.

    immutableString = immutableString + "World";

    // We are now appending "World" to the existing value.

    This looks like we're mutating the string 'immutableString', but we're not. Instead:

    On appending the "immutableString" with a string value, following events occur:

    1. Existing value of "immutableString" is retrieved
    2. "World" is appended to the existing value of "immutableString"
    3. The resultant value is then allocated to a new block of memory
    4. "immutableString" object now points to the newly created memory space
    5. Previously created memory space is now available for garbage collection.
    0 讨论(0)
提交回复
热议问题