There are two types (actually more, but for the problem at hand only these two are important):
-
flat strings are immutable arrays of characters
-
cons strings are pairs of strings, result of concatenation.
If you concat a and b you get a cons-string (a, b)
that represents result of concatenation. If you later concat d
to that you get another cons-string ((a, b), d)
.
Indexing into such a "tree-like" string is not O(1) so to make it faster V8 flattens the string when you index: copies all characters into a flat string.