Inline style vs class

JavaScript performance comparison

Test case created by Marc

Preparation code

<style>
.foo { background-color: red; }
.bar { font-size: 16px; }
</style>
<div id='container'>
</div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  // We have 100 elements with formatting
  var elements = [], styles = [];
  for(var i = 0; i < 100; i++) {
    var e = $('<div>text</div>');
    $('#container').append(e);
    elements.push(e[0]);
    styles.push(e[0].style);
  }

};

Benchmark.prototype.teardown = function() {
  $('#container').empty();
  elements = [];
  styles = [];

};
</script>

Preparation code output

<style> .foo { background-color: red; } .bar { font-size: 16px; } </style> <div id='container'> </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
Set font size by class
for(var i = 0; i < elements.length; i++) {
  elements[i].className = 'bar';
}
pending…
Set font size by inline style
for(var i = 0; i < styles.length; i++) {
  styles[i].fontSize = '16px';
}
pending…
Set background color by class
for(var i = 0; i < elements.length; i++) {
  elements[i].className = 'foo';
}
pending…
Set background color by inline style
for(var i = 0; i < styles.length; i++) {
  styles[i].backgroundColor = 'red';
}
pending…

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

Compare results of other browsers

0 Comments