| Task | To demonstrate the browsers performance in dependence of HTML coding. |
Everyday dealing with creating and animating web sites I began to notice a very unpleasant peculiarity — one at the same JavaScript code works with the different speed on different sites. The cause of such strange behavior was found very promptly: speed of a script execution depended directly on the way the site was HTML coded. Searching of solutions of the arised problems was quite exciting but unfortunately I couldn't share the results of my investigations because of the local character of all the solutions. For example a certain site was working slowly but it was enough to change one construction to another for it to work twice quicker. Getting such an explanation the only thing a reader can do is to be happy for me as there is very little practical benefit from the offered solutions. This fact became the first reason of my researching of the up-to-date browsers general problem places.
The second reason is buzz around a new versions of browsers. I guess there is no secret in the fact that JS code in browsers works quite slowly (in comparison with, for example, the same program written in C++). Developers realized this comparatively recently and started rewriting JS engines of their products for increasing of their performance (V8 in Google Chrome, TraceMonkey in Firefox and so on). Plenty of tests for their engines performance measurement appeared in the Internet. The most well known of them are SunSpider and Dromaeo. This tests are of quite high quality but many users just don't pay attention that these are namely JavaScript Performance Test and not Browser Performance Test. In my opinion their results are far not objective for up-to-date web applications. Is it correctly to assert that one browser is going to be faster then another one in all possible cases?
Let's look at an example. In Dromaeo there is such a test as DOM style. I am not going to pay match attention to it's implementation, I just want to mention that it selects all div elements from the page and and tries to change their color and display properties (I interested exactly in a style modification). And what is going to happen if we put this elements in other environment (i.e. on a page HTML coded in another way)? Thus we have a different result. And what if this element will be not div but an image? It will be already the third result. And what if certain CSS styles will be set to this elements? So you've got it :-)
As the result performance of a web application depends not only on JS engine but also on the browser engine. To demonstrate this I created several simple tests.
| Note | You should in no way treat the above mentioned examples as another browser performance test. They were indicated only to demonstrate how speed of an application performance depends on it's HTML coding. The presented examples cannot be treated as "just one more speed test". It task just demonstrate the dependency of application working speed from HTML code. |
For a long time I have been interested what works faster: an element with position: relative or position: absolute?
![]() |
position:relative |
![]() | position:absolute |
As you can see from the results, Opera is the only browser which performance depends on the way the element being modified is positioned.
In the Internet one can often meet advice to decrease amount of elements in DOM tree. There are such uncertain arguments said in their favour as "Why garbage DOM?", "The less elements the quicker site is loaded" and so on.
Lets check how this affects performance. For running this test I wrapped each word in the content block with a span element. As the result the total amount of DOM elements on the page is 942. Compare (warning: Russian :-):
In this test the examples were compared with the first test but not with each other. We just want to demonstrate dependency of performance from the quantity of elements but not dependency from the way of the elements positioning.
![]() | This test | ![]() | position: relative vs position: absolute |
|
This test |
|
position: relative vs position: absolute |
So small amount of elements in DOM tree is not only aesthetics but significant increasing of up-to-date browsers performance.
| Note | There is no need to increase amount of elements in the document tree without real necessity. |
Now lets make the previous test a little bit more complicated: lets add to all span wrappers CSS property border: 1px solid #fff and see how it affects performance. Keeping in mind that a user should see the same result in all the tests. Lets block border effect with the help of margin: -1px property.
|
This test |
|
position: relative vs position: absolute with big amount of elements |
position:absolute
![]() |
This test |
![]() | position: relative vs position: absolute with big amount of elements |
At the first look Firefox has significant failures in performance, but the overall result does not differ a lot from the previous test.
I really believe that the guys from Microsoft will get rid of such failure in performance in the final version of their new browser.
Using analogy with the test of big amount of elements lets check dependence depth of tree on performance. For this purpose I created a structure consisted of 30 nested in each other elements. In the first case I wrapped the whole template with this structure and in the second case just appended this structure to the end of the template (to normalize amount of page elements). To justify appearance of these elements (they are added not for fun),
I set their CSS styles margin: -1px; padding: 1px; width: 100%;
| Deep tree |
| Shallow tree |
| Note | A shallow tree looks good and improves site performance. |
What is faster — a nontransparent image with CSS opacity property or a semitransparent image?
IE6 was excluded from the test on purpose: the only way to show a semitransparent image to use filter AlphaImageLoader which increases animation performance by itself.
![]() | opaque |
| transparent |
Definetly animation with a semitransparent image is much smoother in up-to-date browsers than animation of a nontransparent image with opacity property. Speed increase of a new WebKit is impressive.
Despite not bad result, animation in Google Chrome works rather slow.
Task: to put an image as background with animation running on top of it. What is going to work faster: obvious CSS background-image property or img element?
| Background |
![]() | Image |
As you can see from the examples obvious (for such task) use of background-image resulted in serious decrease of performance in both Safari and Opera. And the problems were indicated namely in the Mac versions of this browsers.
Despite the high result animation in Opera (Mac) looks very unpleasant (strong jerks are visible).
In reality in some cases img works faster than background-image also in Firefox (in this test it is not noticeable).
| Note | Using img instead of background-image can dramatically improve performance. |
What will happen if background image from the previous test will be enlarged on 1 pixel in HTML?
| Normal state | ![]() | Stretched state (+1 pixel in width) |
It looks like up-to-date browsers are not able meanwhile to optimize such tasks and redraw stretched image with the every change on the page. Problems appear namely in those browsers that smooth stretched images.
Even Firefox 3 page scroll goes slow on big images.
| Note | It is possible to switch on image aliasing in IE7: -ms-interpolation-mode: nearest-neighbor | bicubic. But it reduces IE7 performance approximately in 2 times. |
As seen from the above mentioned tests situation with up-to-date browsers is not definite. At the end of this article I would like to summarize all advice that can help with a project optimization.
This article was written by Sergey Chikuyonok front-end developer and author of Good Sense in Web Development blog.
I agree with Tim above...soome padding would make reading a bit more enjoyable....GREAT content though, thanks!
Great work..
the figures could be little more self-explaining by adding the unit and explaining the red highlighting.
Thanks
Hello,
Great analysis! I am doing some research regarding browser detection. See my browserrecon project:
http://www.computec.ch/projekte/browserrecon/
Were you able to determine any influence of the rendering regarding the download queue/speed?
Regards,
Marc
Amazing ,how can he do that!
I like this post. It help me
pretty! I wish you can keep
Thanks for sharing the post!
I can use it in
That's very nice plugin! I
These tips help me a lot!
Thanks for sharing the post!
Very good plugin! I like it!
it does work - as someone
Good read, well researched. I only have 1 request: p{padding:5px 0;} ;)