addClass vs. css inline style

JavaScript performance comparison

Test case created by eric

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<style>
  #test{color:green}; #test.testing{color:red}; #test.testing-complex{color:red; font-style:italic;
  font-weight:bold; text-decoration:underline; font-size:20px; line-height:30px; position:relative;
  top:3px; left:3px;}
</style>
<div id="test">
  TEST
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var $test = $('#test');

};

Benchmark.prototype.teardown = function() {
  $test.removeAttr('class').removeAttr('style');

};
</script>

Preparation code output

<style> #test{color:green}; #test.testing{color:red}; #test.testing-complex{color:red; font-style:italic; font-weight:bold; text-decoration:underline; font-size:20px; line-height:30px; position:relative; top:3px; left:3px;} </style> <div id="test"> TEST </div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
addClass simple
$test.addClass('testing');
pending…
inline style simple
$test.css('color', 'red');
pending…
inline style complex
$test.css({
  "color": "red",
  "font-style": "italic",
  "font-weight": "bold",
  "text-decoration": "underline",
  "font-size": "20px",
  "line-height": "30px",
  "position": "relative",
  "top": "3px",
  "left": "3px"
});
pending…
addClass complex
$test.addClass('testing-complex');
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.

0 Comments

TEST