views:

290

answers:

9

I am developing a jQtouch app and each request done via ajax creates a new div in the document for the loaded content. Only a single div is shown at any one time.

How many div's can I have before the app starts getting unresponsive and slow?

Anyone have any ideas on this?

EDIT: Its an iPad app running on Safari, and it would be less than 1000 div's with very basic content

A: 

5.

It really depends on the javascript rendering engine of the browser you are using. If we can see some example code, we can tell more about your problem.

Ólafur Waage
+1  A: 

Without defining a particular environment, it's not possible to answer your question.

And even then, anything anyone tells you is just a guess. You need to do your own testing on real-world configurations with different browsers and hardware. You'll also need to establish some performance benchmarks to decide what "too slow" even means.

LBushkin
A: 

It is going to depend on the memory limits set by the browser. Why can't you recycle DIVs?

Fletcher Moore
+2  A: 

To be honest, if you really need an absolute answer to this question, then you might want to reconsider your design.

No answer given here will be right, as it depends upon many factors that are specific to your application. E.g. heavy vs. little CSS use, size of the divs, amount of actual graphics rendering required per div, target browser/platform, number of DOM event listeners etc..

Just because you can doesn't mean that you should! :-)

mdma
+1  A: 

I've had tens of thousands, maybe even a hundred thousand divs, on screen at once. Performance is either fine, or bad, depending on:

Parsed from HTML or generated Dynamically in JavaScript?

Parsed from HTML means you have a LARGE html source, and this can make browsers hang. Generated in JS is surprisingly fast, even on Internet Explorer, which is the slowest of all browsers for JS.

Neil N
Interesting... that makes sense, but in my experience, nothing freezes the browser like a lot of javascript. So the challenge is to break up the generation scrip and make sure it yields to the UI in between.
harpo
+1  A: 

As others have said, there's really no answer.

However, in this talk about the Google Maps API version 3, the speaker brings up the number ten thousand several times, as a basic threshold for browser unhappiness.

http://code.google.com/apis/maps/documentation/javascript/

harpo
A: 

The only time I've seen a modern browser slow down due to too many DOM elements was when checking out a script that tried to "paint" using 1x1 divs, absolutely positioned, with a background color. Those were enough to cripple Firefox.

Tesserex
A: 

I've been able to add several thousand divs without a problem. Depends on what you'll be doing afterwards, of course, and the memory on the client machine. Everyone else is right about that.

As Harpo said, 10K is probably a good ceiling. At one time, I noticed speed problems starting at about 4K divs, but hardware has improved since then.

And, as Neil N said, adding the divs via scripting is better than having a huge HTML source.

And, to answer Harpo's comment, one way to "break it up" so that JS doesn't lock the page and produce a "page is running slowly" error is to call a timer at the end of each "add a div" routine, and the timer in turn calls your "add a div" function again.

Now, MY question is: is it possible to "paint" so that you don't need to add thousands of divs? This can be done with the canvas tag with some browsers, but I don't think it's possible with VML (the excanvas project) on IE. Or is it? I think VML "paints" by adding new elements to the DOM, at which point you may as well use DIVs, unless it's a simple shape.

Is it possible to alter the source of an image via scripting? (the image in the DOM, of course -- not the original image on the server.)

Jay
A: 

A colleague of mine wrote a PHP script that duplicated DIVs until Firefox was no longer usable. His conclusion was it took 20,000 DIVs (and I remind you these DIVs had very little data inside of them) until Firefox would just not function properly.

On **his** computer with however much memory and CPU power it had at the time. See the four month old accepted answer to this question.
David Dorward