<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div class="testElement"></div>
function $q(selector, context) {
return (context || document).querySelectorAll(selector);
}
var el = $(".testElement")[0];
var className = el.className;
var el = document.getElementsByClassName('testElement');
var className = el.className;
var el = document.querySelector('.testElement');
var className = el.className;
var el = $q(".testElement");
var className = el.className;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery | |
Vanilla JS | |
Vanilla JS querySelector | |
VanillaJS custom selector |
Test name | Executions per second |
---|---|
jQuery | 524879.0 Ops/sec |
Vanilla JS | 1702223.4 Ops/sec |
Vanilla JS querySelector | 2181233.5 Ops/sec |
VanillaJS custom selector | 999783.9 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Overview
The benchmark compares four approaches to select an HTML element using JavaScript:
document.getElementsByClassName()
document.querySelector()
Library used in the benchmark
In this case, the custom selector library is called "VanillaJS". It's a custom implementation of the querySelector
function, likely developed by the same team as MeasureThat.net.
According to the MeasureThat.net documentation, VanillaJS is a JavaScript library that provides a similar API to jQuery but with a more lightweight and efficient implementation. The purpose of using this library is to test its performance compared to other approaches.
Options being compared
The four options being tested are:
document.getElementsByClassName()
: Using the native getElementsByClassName
method, which returns a NodeList of elements that match the specified class name.document.querySelector()
: Using the native querySelector
method to select an element by its CSS selector.querySelector
provided by VanillaJS.Pros and cons of each approach
Here's a brief summary of the pros and cons of each approach:
document.getElementsByClassName()
: Pros: Native method, no additional library required, simple implementation. Cons: Returns a NodeList (which is an array-like object), requires explicit iteration to access individual elements.document.querySelector()
: Pros: Efficient and fast, returns a single element or null if not found. Cons: Might be slower than other approaches for very large datasets.Other considerations
Alternatives
If you need to select an HTML element using JavaScript, other alternatives include:
document.querySelectorAll()
: A newer method that returns a NodeList of all matching elements.Element.prototype.matches()
: Introduced in modern browsers (Chrome 70+), this method provides a more concise way to match elements against CSS selectors.In conclusion, the MeasureThat.net benchmark helps evaluate the performance of different approaches to select an HTML element using JavaScript. Understanding the pros and cons of each approach can aid in making informed decisions about which library or method to use for specific projects.