Reputation: 2124
today I've a question. Is faster to load a web page designed from static html example:
<html>
<head>
<title>Web page</title>
</head>
<body>
<p>Hi community</p>
</body>
</html>
Or by static Javascript with DOM? Example with document.createElement("...")
Upvotes: 2
Views: 1404
Reputation: 5542
Populating HTML statistically is much faster than populating through javascript.
The HTML document gets downloaded
The parsing of the HTML document starts
HTML Parsing reaches <script src="jquery.js" ...
jquery.js is downloaded and parsed
HTML parsing reaches <script src="xyz.js" ...
xyz.js is downloaded, parsed and run
HTML parsing reaches <link href="abc.css" ...
abc.css is downloaded and parsed
HTML parsing reaches <style>...</style>
Internal CSS rules are parsed and defined
HTML parsing reaches <script>...</script>
Internal Javascript is parsed and run
HTML Parsing reaches <img src="abc.jpg" ...
xyz.jpg is downloaded and displayed
Parsing of HTML document ends
So, if you have static html loading with javascript will be overhead for page rendering.
Upvotes: 3
Reputation: 523
Like others said, it is always faster to output static HTML rather than generating it dynamically using Javascript/JQuery. However, sometimes the content cannot be served directly and generating the HTML dynamically is the only choice. I have worked on a few applications where this was the case. In general, generate static HTML whenever you can.
Upvotes: -1
Reputation: 1273
It's faster to send a static html, because the client doesn't have to execute js. Sending complete HTML is also better for search engines, even if google can now execute js.
You can still use js to add elements in your DOM once the page is loaded
Upvotes: 1
Reputation: 397
Serving the HTML directly would be the faster approach, as your browser only have to render the elements, and not manipulate the dom :)
Upvotes: 3