Fastest Way To Append A Dynamic JavaScript Class

JavaScript performance comparison

Test case created by Donovan Glover

Info

https://twitter.com/toddmotto/status/307237792600162304

Preparation code

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>Test Page</title>
 </head>
 <body>
  <div class="wrapper">
   <p>some random text</p>
  </div>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 </body>
</html>
 

Preparation code output

Test Page

some random text

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Pure JavaScript
document.body.className = document.body.className.replace("no-js","js");
pending…
jQuery toggleClass()
$("body").toggleClass("no-js js");
pending…
jQuery .removeClass().addClass()
$("body").removeClass("no-js").addClass("js");
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment