This post originated from an RSS feed registered with Ruby Buzz
by Amos King.
Original Post: Change Classes not Styles
Feed Title: DirtyInformation
Feed URL: http://feeds.feedburner.com/Dirtyinformation
Feed Description: Information about Ruby/Rails/JRuby/WebDevelpoment/whatever.
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
Opera 10.10
changing style:554
changing class:111 - 80% faster
I really didn't expect those kinds of results. Kind of amazing!