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

前端 未结 1 1788
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 10:49

I had a need to display HTML text inside my iOS app. I have decided I will use the built-in method on NSAttributedString, initWithData:options:document

相关标签:
1条回答
  • 2020-12-08 11:18

    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.

    0 讨论(0)
提交回复
热议问题