classList.contains Vs IndexOf

Benchmark created by Levi Hackwith on


Preparation HTML

<div id = "my-element" class = "a b c d e f g">Hello World</div>

Setup

function hasClass(element, cls) {
        return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
    var myElement = document.getElementById('my-element');

Teardown


    myElement = undefined;
  

Test runner

Ready to run.

Testing in
TestOps/sec
classList
myElement.classList.contains("g");
ready
IndexOf (via hasClass method)
hasClass(myElement, "g");
ready

Revisions

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

  • Revision 1: published by Levi Hackwith on