TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Two techniques for massively faster JavaScript

44 点作者 techdog大约 16 年前

4 条评论

axod大约 16 年前
&#62;&#62; "How? Use innerHTML wherever possible. "<p>Terrible advice IMHO. True, innerHTML is faster in IE, so if you care about IE being slow (I don't), use innerHTML. However, innerHTML is buggy, has a bunch of caveats, and just looks incredibly ugly.<p><a href="http://www.quirksmode.org/dom/innerhtml.html" rel="nofollow">http://www.quirksmode.org/dom/innerhtml.html</a><p>On non-IE browsers, the difference in speed is negligible, and the code is far nicer using dom methods. (My browser SF/OSX reports 20ms for innerHTML, 25ms for DOM).<p>You also have useful references to nodes you might need later which makes code nicer and more optimum. For example, you could do innerHTML=foo, then use getElementByID later to update one of the nodes in foo. But using the dom methods, you can just save the ref to the node when you create it yourself. You can also attach any event handlers easily as you go, rather than trying to cram them in the HTML (Even more eugh) or set them up afterwards.
评论 #581294 未加载
评论 #581516 未加载
bsaunder大约 16 年前
The fact that most js frameworks willfully ignore #2 has been one of the reasons I've avoided them. Doing a lot of DOM manipulations in code feels about as efficient as making tons of system calls to an OS. Much better to do a few larger writes than many small ones.<p>I've become a big fan of JST: <a href="http://code.google.com/p/trimpath/wiki/JavaScriptTemplates" rel="nofollow">http://code.google.com/p/trimpath/wiki/JavaScriptTemplates</a> . Send over template snippets as strings and use JST to put them together. With far fewer innerHTML replacements along the way.
评论 #581641 未加载
评论 #581590 未加载
themanual大约 16 年前
Also for a faster Javascript you need to avoid script blocking downloading content. See <a href="http://www.webdigi.co.uk/blog/2009/avoid-javascript-blocking-content-download-on-your-website-during-page-load/" rel="nofollow">http://www.webdigi.co.uk/blog/2009/avoid-javascript-blocking...</a>
smwhreyebelong大约 16 年前
x.innerHTML is a DOM operation too. I would think you want to differentiate between doing an "innerHTML" operation and "other DOM operations"<p>That said, constructing HTML as a string and using one innerHTML call is much faster than doing createNode/appendChild multiple times. DOM operations are definitely the bottleneck when it comes to performance : The less of them you do, the better off you are. (I learnt my lesson the hard way)
评论 #581373 未加载