Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

But why's the browser DOM still so slow after 10 years of effort?

Tags:

javascript

dom

The web browser DOM has been around since the late '90s, but it remains one of the largest constraints in performance/speed.

We have some of the world's most brilliant minds from Google, Mozilla, Microsoft, Opera, W3C, and various other organizations working on web technologies for all of us, so obviously this isn't a simple "Oh, we didn't optimize it" issue.

My question is if i were to work on the the part of a web browser that deals specifically with this, why would I have such a hard time making it run faster?

My question is not asking what makes it slow, it's asking why hasn't it become faster?

This seems to be against the grain of what's going on elsewhere, such as JS engines with performance near that of C++ code.

Example of quick script:

for (var i=0;i<=10000;i++){     someString = "foo"; } 

Example of slow because of DOM:

for (var i=0;i<=10000;i++){     element.innerHTML = "foo"; } 

Some details as per request:

After bench marking, it looks like it's not an unsolvable slow issue, but often the wrong tool is used, and the tool used depends on what you're doing cross-browser.

It looks like the DOM efficiency varies greatly between browsers, but my original presumption that the dom is slow and unsolvable seems to be wrong.

I ran tests against Chrome, FF4, and IE 5-9, you can see the operations per second in this chart:

enter image description here

Chrome is lightning fast when you use the DOM API, but vastly slower using the .innerHTML operator (by a magnitude 1000-fold slower), however, FF is worse than Chrome in some areas (for instance, the append test is much slower than Chrome), but the InnerHTML test runs much faster than chrome.

IE seems to actually be getting worse at using DOM append and better at innerHTML as you progress through versions since 5.5 (ie, 73ops/sec in IE8 now at 51 ops/sec in IE9).

I have the test page over here:

http://jsperf.com/browser-dom-speed-tests2

What's interesting is that it seems different browsers seem to all be having different challenges when generating the DOM. Why is there such disparity here?

like image 522
Incognito Avatar asked Jul 25 '11 13:07

Incognito


People also ask

Why are DOM updates slow?

Because the DOM tree is usually quite large and there's also a stylesheets tree that needs to be applied to possibly every element, re-rendering takes quite some time.

Is accessing the DOM slow?

The short answer is that the DOM is not slow. Adding & removing a DOM node is a few pointer swaps, not much more than setting a property on the JS object. However, layout is slow.

How does browser update DOM?

First, the browser combines the DOM and CSSOM into a "render tree," which captures all the visible DOM content on the page and all the CSSOM style information for each node. To construct the render tree, the browser roughly does the following: Starting at the root of the DOM tree, traverse each visible node.


1 Answers

When you change something in the DOM it can have myriad side-effects to do with recalculating layouts, style sheets etc.

This isn't the only reason: when you set element.innerHTML=x you are no longer dealing with ordinary "store a value here" variables, but with special objects which update a load of internal state in the browser when you set them.

The full implications of element.innerHTML=x are enormous. Rough overview:

  • parse x as HTML
  • ask browser extensions for permission
  • destroy existing child nodes of element
  • create child nodes
  • recompute styles which are defined in terms of parent-child relationships
  • recompute physical dimensions of page elements
  • notify browser extensions of the change
  • update Javascript variables which are handles to real DOM nodes

All these updates have to go through an API which bridges Javascript and the HTML engine. One reason that Javascript is so fast these days is that we compile it to some faster language or even machine code, masses of optimisations happen because the behaviour of the values is well-defined. When working through the DOM API, none of this is possible. Speedups elsewhere have left the DOM behind.

like image 174
spraff Avatar answered Sep 30 '22 18:09

spraff