grep is the product of a lot of thought and engineering. There's a great rundown by GNU grep's author, if you're interested [1].
One big takeaway is that grep doesn't simply traverse the string linearly, instead using the Boyer-Moore algorithm [2].
1: https://lists.freebsd.org/pipermail/freebsd-current/2010-Aug...
2: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_sea...
EDIT: Interestingly, there seems to be an internal Boyer-Moore implementation in Golang's strings package[3], though it only appears to be used in strings.Replace("")[4]