The document.getElementById() or jQuery(#id) method returns the element that has the ID attribute with the specified value.
This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from, an element on your document.
Recently I did some research and found that there are many ways to access HTML element, but I'm concerned about the overall performance in terms of speed & browser compatibility. Hence, I created a JavaScript Benchmark Test based on 13 common methods and tested on 5 popular browsers.
Test - Environment
- CPU: Intel i7-4770 @3.4GHz (8 cores)
- Memory: 8GB DDR3
- Hard Disk: Intel 535 120GB SSD (Read: 540MBps, Write: 450MBps)
- Network Card: Gigabit
- Internet Bandwidth: 100Mbps (Download: 100Mbps, Upload: 100Mbps)
- Platform: Windows 10 (64-bit)
Test - HTML DOM methods (Pure JavaScript vs jQuery)
- getElementById()
- getElementsByClassName()
- getElementsByTagName()
- querySelector(#id)
- querySelector(.id)
- querySelectorAll(#id)
- querySelectorAll(#id)
- jQuery(#id)
- jQuery(.id)
- jQuery(tag#id)
- jQuery(tag.id)
- jQuery + getElementById
- jQuery + getElementByClassName
Test - Modern Browsers
- IE 11 on Windows 10
- Edge 14 on Windows 10
- Google Chrome 54 on Windows 10
- Mozilla Firefox 49.0.2 on Windows 10
- Opera 41 on Windows 10
- Google Chrome 55 on Windows 10
- Mozilla Firefox 50.1 on Windows 10
- Opera 42 on Windows 10
- Google Chrome 56 on Windows 10
- Mozilla Firefox 51.0.1 on Windows 10
- Opera 43 on Windows 10
Test Result
Test Result (Updated on 27 Dec 2016)
Test Result (Updated on 15 Feb 2017)
Conclusion
Based on the test result, Pure JavaScript document.getElementById() is proven to be the fastest HTML DOM on the planet so far.You may perform the same test on your own environment at: https://www.measurethat.net/Benchmarks/Show/554
Browser Support
document.getElementById() is fully compatible with all browsers.To learn more about document.getElementById(), kindly visit http://www.w3schools.com/jsref/met_document_getelementbyid.asp
No comments :
Post a Comment