ClassList versus ClassName (v6)

Revision 6 of this benchmark created on


Setup

var fooEl = document.createElement("div");
  fooEl.className = ' aaaa bbbb cccc dddd ';
  // with the leading and trailing spaces
  
  var nowhitespaceEl = document.createElement("div");
  nowhitespaceEl.className = 'aaaa bbbb cccc dddd';
  // without the leading and trailing spaces
  
  var rtrim = /^\s+|\s+$/g;

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
className.replace with trim
nowhitespaceEl.className = (' ' + nowhitespaceEl.className + ' ').replace(' ' + 'aaaa' + ' ', ' ').replace(rtrim, '');
ready

Revisions

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