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

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
$li.addClass('test');
$li.removeClass('test');
ready
attr class
$li.attr('class', 'test');
$li.removeClass('test');
ready

Revisions

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