On Mar 30, 11:14 am, Ricardo <ricardob...@gmail.com> wrote:
> No normal browser/computer has enough power to deal with that many
> elements without getting slow. The fastest way to do it would be
> something like this (array join, append a single element, use
> innerHTML directly):
>
> var html = [], i = 0;
> html[i++] = '<div id="container">';
> for(var ln = 500*500;i < ln; i++) {
>    html[i] = '<div></div>';};

If speed is an issue, a do loop is up to 3 times faster in IE and
Firefox (depending on the number of loops) than a for loop:

  var m = '<div></div>';
  var i = 500*500;
  do {
    html[--i] = m;
  } while (i);

Of course displaying the divs by setting the innerHTML property will
likely take far longer than the loop, so the time saved by this
optimisation may be insignificant to the total time taken.  For 1
million for loops, Firefox only takes 133ms on my modest PC, and 70 ms
for the do loop. IE 6 takes a little over 2 seconds and half a second
respectively.


--
Rob

Reply via email to