Improve TokenUtils performance through caching

For #9717.

For testing, you can take https://gist.github.com/MarcelGerber/5c11f1031af292044708, placing your cursor in the script tags and pressing `Ctrl-Space`.
This function was written this way for nested mode support, so we need to make sure that `cm.getModeAt(pos)` supports that. I'm seeing the HTMLUtils InlineEditorProviders "should find style blocks" unit test fail.