Why is there a significant performance increase when using td and tr to wrap thousands of lines of source code in a listing?

六月ゝ 毕业季﹏ 提交于 2020-01-23 12:41:54

问题


I am currently writing a sophisticated syntax highlighter to properly highlight source code listings. In order to test the performance of my statically generated HTML output (not even while dynamically replacing the DOM) I initially used a 3k LoC file which resultend in an 1 MB HTML and noticed unbearable performance degradation on my first implementation. Previously the output was of this form:

<pre>
<code>
<span class="token import">import</span> <span class="token export-all">*</span> <!-- ... -->
<span class="token const">const</span> <span class="token const-var">LIMIT</span> <span class="token operator-assignment">=</span> <!-- ... -->
<!-- ... -->
</code>
</pre>

When loading the generated HTML file locally Chrome was unresponsive, scrolling wasn't really possible and I had to come up with something entirely different. Thus I took a peek at Githubs file view and noticed they were using tables. I was curious and ended up doing the following:

<table>
<tbody>
<tr><td style="white-space: pre;"><span class="token import">import</span> <span class="token export-all">*</span> <!-- ... --></td></tr>
<tr><td style="white-space: pre;"><span class="token const">const</span> <span class="token const-var">LIMIT</span> <span class="token operator-assignment">=</span> <!-- ... --></td></tr>
<!-- ... -->
</tbody>
</table>

The performance was infinitely better to my surprise and everything was smooth besides some minor issues with selecting some lines which is a CSS issues. Generally I was curious why using a table is improving performance this much. Furthermore I'd like to know whether there are more suggested optimizations I can take a look at.

来源:https://stackoverflow.com/questions/58471917/why-is-there-a-significant-performance-increase-when-using-td-and-tr-to-wrap-tho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!