
Elixir string operations seem slow (and why it's a good thing)
·1597 words·8 mins
I personally hate it when people post clickbait titles and take their sweet time getting to the point, so let’s do this first:
TL;DR: Some Elixir string operations, most notably String.at/2 work in linear time, as opposed to constant time, like the intuition might suggest. This is because the String module is UTF-8 aware. UTF-8 encodes characters outside of ASCII with more than one byte, so in order to find the n-th character in a string you need to process it from the beginning, you can’t just use an offset in memory.