Change Classes not Styles

November 4th, 2010

I've been working with some javascript lately and wanted to see if it made a difference if I was changing the class or inline style of an element. I had heard that changing classes was faster, but can it really make a big difference?

YES!!!

I coded up a quick example. Here are the results in ms(all on osx):

  • Safari 5.0.2
    • changing style:123
    • changing class:15 - 87.8% faster
  • Chrome 7.0.517.44
    • changing style:115
    • changing class:16 - 86.1% faster
  • Firefox 3.0.15
    • changing style:333
    • changing class:99 - 70.3% faster
  • Firefox 3.6.12
    • changing style:261
    • changing class:60 - 77% faster
  • Opera 10.10
    • changing style:554
    • changing class:111 - 80% faster

I really didn't expect those kinds of results. Kind of amazing!

Sorry, comments are closed for this article.