straight html or dom generated html objects faster?

A

anagai

Im wondering if generating html objects such as tabels and rows in
javascript is faster than typing the html directly? Seems when you do
it in javascript you have to download alot of code and would slow down
displaying the page. while if you just type the html, it requires less
bandwidth and display faster?

is parsing html to display in browser slower than doing it through dom
to display the same html objects on the page?
 
B

bwucke

anagai said:
Im wondering if generating html objects such as tabels and rows in
javascript is faster than typing the html directly? Seems when you do
it in javascript you have to download alot of code and would slow down
displaying the page. while if you just type the html, it requires less
bandwidth and display faster?

"download a lot of code" is a pretty wrong idea. The code is there. If
the browser creates a line of a table from <tr>..</tr>, it calls the
very same createElement() that you would call to do so from Javascript.

The answer is ambigious though, because while calling DOM functions
from JS to create elements is faster because of skipping the whole
XML/SGML parsing, it is at the same time slower because of launching
userspace javascript program which takes up CPU space. Whether the
speedup or slowdown is stronger depends on lots of factors.

Obviously sending <script>for(var
i=1;i<10000;i++){document.appendChild(createNode('br'))}</script> will
take less bandwidth than sending 10000 BR tags. Which will take shorter
is a different matter though,
is parsing html to display in browser slower than doing it through dom
to display the same html objects on the page?

The objects to be displayed must pass both steps. First to be parsed as
HTML, then the resulting tree rendered as DOM. Likely the former is
slower, but for static HTML both are needed. If you generate elements
with a script, you skip HTML parsing, but then there's whole script
parsing, compilation, checking, execution and it takes time too.
Generally createElement will be faster than document.write() or
..innerHTML (and more reliable too), but most likely
document.getElementById('popup').style.display="block";; after having
created 'popup' with static HTML earlier will be faster than
document.appendChild(popup);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top