Which is better: rendering HTML on server or client in JS?

I have experience in best practice / performance. I am creating an ASP.NET MVC 2 project, and I have several parts of the page that are accessed dynamically either during loading or when interacting with the user.

My question is this: is it better to have the sections displayed in HTML on the server and then just replace the HTML sections or is it better to just get the information as JSON objects and then use JS to create and paste the HTML?

It should be noted that the objects of problems are very simple in nature. An example is the message object, which has an identifier field, a field, a field, a topic field, and a body field that are integer.

Are there any major advantages or disadvantages to any approach? Or is this a preferred example of how to create your application?

+8
performance javascript html asp.net-mvc
source share
4 answers

I do not think this is better; It will depend on your requirements. The question of the border is impossible. Do you use data on the client for further calculations or manipulations, or are you just throwing something to display?

In both cases, you output textual data, although it is easier to represent data structures as JSON more directly than converting data structures to HTML, and it is easier to directly display HTML than JSON.

+1
source share

Consider the following issues:

  • Will there be any advantage of having raw data on the client? In some cases, other parts of the page use data. In these cases, it may make sense to send data by cable.

  • Are there any potential differences in performance? Consider a common pipeline. Sending HTML may be verbose, but faster on the server? Can the displayed HTML be cached on the server?

If none of them pushes you in one direction or another, I choose a more convenient code base. This will depend not only on the specific problem, but also on the skill set of the team.

Bean

+2
source share

Many frameworks have relatively slow rendering libraries (part of the Model-View-Controller architecture view). The reason is because the rendering library has to parse / execute a domain specific view to replace variables, etc.

Depending on the size of the application, it can be much faster if the client browser renders. But moving the View computation to the client can be difficult to make it consistent.

The Google Closure compiler contains a library of templates. Another option is liquid. It has Javascript,. Net and Ruby .

+1
source share

As Jonathon already said, I don’t think there is a simple yes / no answer to your question.

The only factor that has not already been mentioned is that server-side execution is more predictable, while client-side execution is out of control and may vary depending on the browser. This may hardly be a factor on the intranet site, but it can become important if the audience is diverse. Modern Javascript libraries usually (not always) protect us from browser jokes, but older browsers may have specific performance problems (performance really should not be your main criterion, although if you don't try it, and that's awful).

Choosing a solution that, in your opinion, is most comfortably implemented can be very good.

0
source share

Source: https://habr.com/ru/post/649804/


All Articles