jQuery addClass(...) vs attr('class', ...) (v8)

Revision 8 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<ol>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
  <li>
    Item
  </li>
</ol>
<script>
  var $li = $('li');
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
addClass (multiple classes)
$li.addClass('test test1 test2 test3 test4 test5 test6');
$li.removeClass('test');
ready
attr class (multiple classes)
$li.attr('class', 'test test1 test2 test3 test4 test5 test6');
$li.removeClass('test');
ready

Revisions

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