jQuery addClass(...) vs attr('class', ...) vs JavaScript className (v13)

Revision 13 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<ol>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
  <li class="remove">
    Item
  </li>
</ol>
<style>
  .addClass {background-color: yellow}
  .attr {background-color: blue}
  .JS {background-color: red}
</style>

Setup

var $li = $('li'), i;

Test runner

Ready to run.

Testing in
TestOps/sec
addClass
$li.removeClass('remove').addClass('addClass');
ready
attr class
$li.attr('class', 'attr');
ready
JS className
for (i = 0; i < $li.length; i += 1) {
  $li[i].className = 'JS';
}
ready

Revisions

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