Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why exactly is server side HTML rendering faster than client side?

I am working on a large web site, and we're moving a lot of functionality to the client side (Require.js, Backbone and Handlebars stack). There are even discussions about possibly moving all rendering to the client side.

But reading some articles, especially ones about Twitter moving away from client side rendering, which mention that server side is faster / more reliable, I begin to have questions. I don't understand how rendering fairly simple HTML widgets in JS from JSON and templates is a contemporary browser on a dual core CPU with 4-8 GB RAM is any slower than making dozens of includes in your server side app. Are there any actual real life benchmarking figures regarding this?

Also, it seems like parsing HTML templates by server side templating engines can't be any faster than rendering same HTML code from a Handlebars template, especially if this is a precomp JS function?

like image 567
mvbl fst Avatar asked Oct 28 '12 04:10

mvbl fst


People also ask

Why server-side rendering is faster than client-side rendering?

Server-side rendering allows developers to pre-populate a web page with custom user data directly on the server. It is generally faster to make all the requests within a server than making extra browser-to-server round-trips for them. This is what developers used to do before client-side rendering.

Is server-side rendering better than client-side?

Between the two options, server-side rendering is better for SEO than client-side rendering. This is because server-side rendering can speed up page load times, which not only improves the user experience, but can help your site rank better in Google search results.

Why is server-side rendering better?

Some server-side rendering advantages include: A server-side rendered application enables pages to load faster, improving the user experience. When rendering server-side, search engines can easily index and crawl content because the content can be rendered before the page is loaded, which is ideal for SEO.

Why is client-side rendering slow?

With client-side rendering, the initial page load is going to be slow. Because communicating over the network is slow, and it takes two round trips to the server before you can start displaying content to the user. However, after that, every subsequent page load will be blazingly fast.


3 Answers

There are many reasons:

  1. JavaScript is interpreted language and is slower than server side (usually done in compiled language)
  2. DOM manipulation is slow, and if you are manipulating it in JS it results in poor performance. There are ways to overcome this like preparing your rendering in text then evaluating it, this might in fact gets you as close to server side rendering.
  3. Some browsers are just too slow, especially old IE
like image 131
albattran Avatar answered Oct 04 '22 11:10

albattran


  • Performance of compiled language versus interpreted javascript
  • Caching, ie - serving up the exact same page another user has already requested, this removes the need for each client to render it. Great for sites with huge traffic - ie news sites. Micro-caching can even provide near real-time updates, yet serve significant traffic from the cache. No need to wait for client rendering
  • Less reliance on users with old computers or slow / crippled browsers
  • Only need to worry about rendering, less reliance on how different browsers manage DOM (reliability)

But for a complex UI, client side rendering of interactions will provide a snappier user experience.

It really depends on what performance you're trying to optimise, and for how many users.

like image 22
Pete - MSFT Avatar answered Oct 04 '22 09:10

Pete - MSFT


To run code on the client side it first has to be loaded. Server side code is just loaded when the server start, whereas the client code must potentially be loaded every time the page is. In any case the code must be interpreted when loading the page, even if the file is already cached. You might also have caching of JS parse trees in the browser, but I think those are not persisted, so they won't live long.

This means that no matter how fast JavaScript is (and it is quite fast) work has to be performed while the user waits. Many studies have shown that page loading time greatly affects the users perception of the sites quality and relevance.

Bottom line is that you have 500ms at the most to get your page rendered from a clean cache on your typical developer environment. Slower devices and networks will make that lag just barely acceptable to most users.

So you probably have 50-100ms to do things in JavaScript during page load, all of it, grand total, which means that rendering a complex page, well, not easy.

like image 34
Henrik Vendelbo Avatar answered Oct 04 '22 11:10

Henrik Vendelbo