ClassList versus ClassName (v5)

Revision 5 of this benchmark created on


Setup

var fooEl = document.createElement("div");
  
  fooEl.className = ' aaaa bbbb cccc dddd '; 
  //note the leading and trailing spaces

Teardown



            fooEl = null;
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
classList.remove
fooEl.classList.remove('aaaa');
ready
className.replace
fooEl.className = fooEl.className.replace('aaaa ', ' ');
ready
classList.add
fooEl.classList.add('zzzz');
ready
className +=
//apple to apple comparison requires a check for duplicate class... to use speedy indexOf check requires fragile leading and trailing space convention in className strings to ensure you're not matching a piece of the className (this could also be done with regexp word boundaries)...

if( fooEl.className.indexOf(' zzzz ') === -1 ){
    fooEl.className += 'zzzz ';
}
ready
className.replace (many) + cleanup
fooEl.className = fooEl.className.replace('aaaa ', ' ').replace('bbbb ', ' ').replace('cccc ', ' ').replace('dddd ',' ').replace('  ',' ');

//the final replace is ensure proper clean up of all the extra spaces left behind, keeping one for the leading-trailing space convention mentioned above
ready
classList.remove (many)
fooEl.classList.remove('aaaa');
fooEl.classList.remove('bbbb');
fooEl.classList.remove('cccc');
fooEl.classList.remove('dddd');
ready

Revisions

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