Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
$.hasClass vs RegExp vs indexOf vs classList 2
(version: 0)
Comparing performance of:
$.hasClass vs RegExp vs Dry RegExp vs indexOf vs classList vs hasClassString vs indexOf more than operator
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <div class="foobar" id="node">Hello</div>
Script Preparation code:
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; };
Tests:
$.hasClass
var hasClass = $(node).hasClass(className);
RegExp
var hasClass = rx.test(node.className);
Dry RegExp
var drx = new RegExp('\\b' + className + '\\b'); var hasClass = drx.test(node.className);
indexOf
var hasClass = node.className.indexOf(className) !== -1;
classList
var hasClass = node.classList.contains(className);
hasClassString
var hasClass = hasClassString(node, className);
indexOf more than operator
var hasClass = node.className.indexOf(className) > -1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
$.hasClass
RegExp
Dry RegExp
indexOf
classList
hasClassString
indexOf more than operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
$.hasClass vs RegExp vs indexOf vs classList
$.hasClass vs RegExp vs indexOf vs classList
$.hasClass vs RegExp vs indexOf vs classList
$.hasClass vs RegExp vs indexOf vs classList
Comments
Confirm delete:
Do you really want to delete benchmark?