getElementById vs $('#element') vs window.document.l
getElementById vs $('#element') vs window.document.links[n]
我认为速度的顺序是:
- 元素数组:文档。链接[0]//为0,正确的索引
- 原生JavaScript获取Id: document.getElementById('linkid')
- JQuery选择器:美元(# linkid)
我说的对吗?
是否有一种工具或方法来测试这种怀疑的性能?
谢谢!
在/if jsperf重新上线之前,您可以使用benchmark.js。这是jsperf内部用来衡量性能的。
如果你担心性能,很可能javascript优化不是答案,但上下文是关键。
。使用document.getElementById
和$('#')
的区别并不在于加载和不加载jQuery。
在你的例子中,你对性能的顺序是正确的,但是你的第一个案例document.links[0]
是一个糟糕的方式来写代码,它更难维护和阅读getElementById
和不灵活的html/dom的变化。
相关文章:
- Javascript events: window.event vs argument reference (funct
- Javascripts commands: window.alert vs console.log
- window.outerWidth VS $(window).outerWidth()
- 移动野生动物园:frame.src vs window.location
- window.location.pathname Chrome vs Mozilla
- window.find() in Firefox vs Chrome
- window.onload vs jQuery(document).ready
- setTimeout(0) vs window.postMessage vs MessagePort.postMessa
- JSON 解析错误:window.onload vs XMLHttpRequest
- this.globalvar vs window.globalvar
- 美元(窗口).load vs. window.addEventListener
- IE8+, window.open(url/#/frag,'_parent') vs. parent.l
- 使用 document.ready vs window.load 来加载延迟的内容
- window.location.href vs window.location.replace vs window.lo
- window.localStorage vs chrome.storage.local
- Window vs Var来声明变量
- FileReader vs. window.URL.createObjectURL
- getElementById vs $('#element') vs window.document.l
- 全局var混淆:var a vs window.a
- window.onbeforeunload vs window.print in Internet Explorer