jQuery 2.1.0 hasClass vs. addClass (v3)

Revision 3 of this benchmark created on


Description

Test the performance of toggling classes in jQuery 2.1.0 vs. checking if a class exists before (possibly unnecessary) manipulation.

Preparation HTML

<div class="test blue"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js">
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
hasClass + addClass
var $test = $('.test');
if ($test.hasClass('blue')) { $test.addClass('blue'); }
ready
addClass
var $test = $('.test');
$test.addClass('blue');
ready
hasClass only
var $test = $('.test');
$test.hasClass('blue');
ready

Revisions

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