String.replace() implementation is very poor

Indeed, the problem lies mainly in splitToSequence(), AFAICS it actually creates substrings of the original string.
Of course the JIT compiler could magically remove these substring allocations and work directly with substrings indexed on the original string without actually allocating the substring, but currently that is not the case.

This kind of optimization would be a lot easier if the language provided a SubString class that doesn’t have any string allocation but is just a range in another string. We actually are using this approach for ByteArrays, we’ve created a SubByteArray class that is a range in another ByteArray and can be used to work with substrings without actually copying the string data.