3 ms·
String builders originated in languages with immutable strings making code using something like "foo += bar" in a loop very expensive due to the need to allocat
by int_19h 4y ago
String builders originated in languages with immutable strings making code using something like "foo += bar" in a loop very expensive due to the need to allocate a new string on every iteration. A string builder is basically a mutable string that can be built in-place efficiently and converted to a proper immutable string at the end. It is purely a performance thing, and there are no Unicode issues when concatenating valid Unicode strings (i.e. sequences of codepoints).
- tsimionescu 4y agoNote that some kind of string builders are necessary for efficient repeated concatenation both in languages with immutable strings and in languages with 0-terminated strings. Using strcat() repeatedly in C for example will mean that the string is being read over and over again to find the end, making an O(n) loop actually O(n²).
- lolinder 4y agoRight, but that's still a performance optimization and not anything like what OP is implying.
- tsimionescu 4y agoYes yes, OP had some really strange ideas. I was just adding some extra info to GP's answer, not trying to contradict them.