ITeye博客

getElementByTagName 与 querySelectorAll

deng131 · 2011-07-14 11:29 · javascript · Web前端 · Markdown

虽然网上有中文翻译但是还是直接看英文有感觉。getElementByTagName 与 querySelectorAll在节点查询时候在效率上区别。

Conclusion

The real reason why getElementsByTagName() is faster than querySelectorAll() is because of the difference between live and static NodeList objects. Although I’m sure there are way to optimize this, doing no upfront work for a live NodeList will generally always be faster than doing all of the work to create a static NodeList. Determining which method to use is highly dependent on what you’re trying to do. If you’re just searching for elements by tag name and you don’t need a snapshot, then getElementsByTagName() should be used; if you do need a snapshot of results or you’re doing a more complex CSS query, then querySelectorAll() should be used.

参考: http://mzhou.me/?p=95119 http://www.w3.org/TR/selectors-api/#queryselectorall http://www.nczonline.net/blog/2010/09/28/why-is-getelementsbytagname-faster-that-queryselectorall/ http://jsperf.com/queryselectorall-vs-getelementsbytagname

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。