Why does the initial call to NSAttributedString with an HTML string take over 100 times longer than subsequent calls? Why does the initial call to NSAttributedString with an HTML string take over 100 times longer than subsequent calls? ios ios

Why does the initial call to NSAttributedString with an HTML string take over 100 times longer than subsequent calls?


That's a really good question. It turns out that (at least for me) it is always slower the first time I call the method, no matter if the debugger is attached or not. Here is why: The first time you parse an HTML-attributed string, iOS loads a whole JavaScriptCore engine and WebKit into memory. Watch:

The first time we run the method (before parsing the string) only 3 threads exist:

screenshot 1

After the string is parsed, we have 11 threads:

screenshot 2

Now the next time we run the method, most of those web-related threads are still in existence:

screenshot 3

That explains why it's slow the first time and fast thereafter.