element.classList vs element.className (v4)

Revision 4 of this benchmark created on


Description

Is it better to use native classList to check if an element is associated to an specific class or just search in the className string?

Preparation HTML

<p id="foo" class="foo bar zen"></p>

Setup

var elm = document.getElementById("foo");
    var className = "bar";
    
    function by_className() {
      return (" " + elm.className + " ").indexOf(" "+className+" ") > -1; //extracted from https://github.com/julienw/dollardom/blob/master/$dom.dev.js
    }
    
    function by_classList() {
      return elm.classList.contains(className);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Testing the className
by_className();
ready
classList.contains
by_classList()
ready

Revisions

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