ClassList versus ClassName

Benchmark created by Steve King on


Preparation HTML

<div id="foo"
></div>
<script>
 fooEl = document.getElementById('foo');
</script><script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

fooEl.className = 'aaaa bbbb cccc dddd';
  jFooEl = jQuery(fooEl);

Test runner

Ready to run.

Testing in
TestOps/sec
Class List
var x = false;
fooEl.classList.add('bbbb');    // duplicate
fooEl.classList.remove('eeee'); // not found
fooEl.classList.add('ffff');    // actually adds
if(fooEl.classList.contains('cccc')) {
  x = true;
}
ready
jQuery Class Name
var x = false;
jFooEl.addClass('bbbb');    // duplicate
jFooEl.removeClass('eeee'); // not found
jFooEl.addClass('ffff');    // actually adds
if(jFooEl.hasClass('cccc')) {
  x = true;
}
ready
Raw Class Name
var indexOf;

if((' ' + fooEl.className + ' ').indexOf(' bbbb ') < 0)
 fooEl.className += ' bbbb';

var classArray = fooEl.className.split(/\s+/),
    indexOf = classArray.indexOf('eeee');

if(indexOf >= 0) {
 classArray.splice(indexOf,1);
 fooEl.className = classArray.join(' ');
}

if((' ' + fooEl.className + ' ').indexOf(' ffff ') < 0)
 fooEl.className += ' ffff';

if((' ' + fooEl.className + ' ').indexOf(' cccc ') >= 0) {
  x = true;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.