I find myself doing stuff like this in somewhat hot paths sometimes:
let head = string.slice(0, index)
let tail = string.slice(index)
It'd be very nice to be able to split at that index in a single call:
let [head, tail] = string.splitAt(index)
Obviously, this is easily abstracted in userland. However, high-perf engines can leverage string representation to make this way faster, and this is the real reason I'm proposing it.
- Instead of just slicing it twice, engines could do a single tree/rope walk to build both the end of the first result and the start of the second result. Halving the number of dependent memory loads (string slicing is heavily bound by memory load latency) in this step and populating both results in the same pass (CPUs can perform the requisite stores concurrently) will result in a massive speed-up.
- Only one index is being resolved instead of 4 -
S.p.substring
is sometimes measurably faster simply due to having less length resolution overhead, so a noticeable speed-up from just this isn't an unrealistic possibility.
I suspect a total speed-up of anywhere from 20% to 100% over two slice ops, though unfortunately I don't have the resources to confirm it at the moment.
Alternatively, just exposing a string splitter object that accepts a source string and supplies chunks of it by requested length would work. And honestly, that might be even better.