jquery add/removeClass vs toggleClass

Benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="test">
</div>

Setup

var el = $('#test');

Test runner

Ready to run.

Testing in
TestOps/sec
add/removeClass
var condition = Math.random() > 0.5;
if (condition) el.addClass('someclass');
else el.removeClass('someclass');
ready
toggleClass
var condition = Math.random() > 0.5;
el.toggleClass('someclass', condition);
ready

Revisions

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