<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div class="foobar" id="node">Hello</div>
var node = document.getElementById('node');
var className = node.className;
var rx = new RegExp('\\b' + className + '\\b');
function hasClassString(e, c) {
var s = e.className, i = s.indexOf(c);
return i != -1 && (s.charCodeAt(i - 1) || 32) == 32 && (s.charCodeAt(i + c.length) || 32) == 32;
};
var hasClass = $(node).hasClass(className);
var hasClass = rx.test(node.className);
var drx = new RegExp('\\b' + className + '\\b');
var hasClass = drx.test(node.className);
var hasClass = node.className.indexOf(className) !== -1;
var hasClass = node.classList.contains(className);
var hasClass = hasClassString(node, className);
var hasClass = node.className.indexOf(className) > -1;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
$.hasClass | |
RegExp | |
Dry RegExp | |
indexOf | |
classList | |
hasClassString | |
indexOf more than operator |
Test name | Executions per second |
---|---|
$.hasClass | 786190.6 Ops/sec |
RegExp | 3424062.2 Ops/sec |
Dry RegExp | 1154993.4 Ops/sec |
indexOf | 4177220.5 Ops/sec |
classList | 3525022.8 Ops/sec |
hasClassString | 2540448.8 Ops/sec |
indexOf more than operator | 4210653.5 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing different approaches to test various aspects of the language. In this explanation, we will break down the provided benchmark definition and individual test cases, discussing what each option tests, their pros and cons, and other considerations.
Benchmark Definition
The benchmark definition is a JSON object that describes the benchmark being run:
{
"Name": "$.hasClass vs RegExp vs indexOf vs classList 2",
"Description": null,
"Script Preparation Code": ...,
"Html Preparation Code": ...
}
This benchmark tests four different approaches to check if an element has a specific class:
$ .hasClass
(jQuery method)RegExp
(regular expression)indexOf
(string indexing)classList
(DOM API)Individual Test Cases
Each test case is defined in the "Benchmark Definition" JSON object:
[
{
"Benchmark Definition": "...",
"Test Name": "$.hasClass"
},
...
]
Here's a brief explanation of each test case:
$ .hasClass
(jQuery method)RegExp
(regular expression)className
property.Dry RegExp
indexOf
className
property.classList
hasClassString
$ .hasClass
, but it's defined in a separate function.indexOf more than operator
indexOf
but uses the "more than" operator ( > -1
) instead of checking for equality.Options Compared
The four main options being compared are:
$ .hasClass
vs RegExp
hasClass
method is generally faster and more efficient than using regular expressions.indexOf
vs classList
classList.contains()
method is faster and more accurate than using string indexing (indexOf
).Dry RegExp
Dry RegExp
) does not provide any significant performance benefits over compiling it.Pros and Cons
Here are some pros and cons for each option:
$ .hasClass
RegExp
indexOf
classList
Other Considerations
$ .hasClass
and classList
) rely on external libraries (jQuery or DOM API), which can impact performance.In conclusion, this benchmark highlights the importance of considering performance, accuracy, and browser support when choosing an approach to test class presence in JavaScript elements.