Get Classes of Element

JavaScript performance comparison

Test case created by Gergő Ertli

Preparation code

<div id="base" class="foo bar">
  <div id="elem" class="foo bar baz">test 1</div>
  <span class="foo bar">test 2</span>
  <span class="baz">test 3</span>
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var elem = document.getElementById('elem')

};
</script>

Preparation code output

<div id="base" class="foo bar"> <div id="elem" class="foo bar baz">test 1</div> <span class="foo bar">test 2</span> <span class="baz">test 3</span> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
className
elem.className.indexOf('bar') >= 0
pending…
classList
elem.classList.contains('bar') >= 0
pending…
getAttribute
elem.getAttribute('class').indexOf('bar') >= 0
pending…

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

0 Comments

test 1
test 2 test 3