简体   繁体   English

前端JS和后端JS之间的性能更好

[英]Better performance between frontend JS and backend JS

I am a frontend guy, but I am working on a project in which I need to process lots of data in my nodeJS backend (my front is reactJS). 我是一个前端人员,但是我正在一个项目中,我需要在nodeJS后端中处理大量数据(我的前端是reactJS)。

But once the data that needed to be processed in the backend is processed, I have the choice of either reprocessing this data in node or in react (knowing that in the end, I need this data in frontend). 但是,一旦处理完需要在后端处理的数据,我就可以选择在节点中还是在响应中重新处理此数据(知道最后,我需要在前端使用此数据)。

Example: An array of links has been created in my backend, but I need to extract a single link from this array, in order to display it in React. 示例:在后端创建了一个链接数组,但是我需要从该数组中提取单个链接,以便在React中显示它。 I have the choice, pass the array to react and process the data there, or do it directly in node. 我可以选择传递数组以进行响应并在那里处理数据,或者直接在节点中进行选择。

Is there a common fashion to fix this dilemma? 有解决此难题的通用方法吗? What should I take into account to make a decision? 我应该考虑什么才能做出决定?

It's not good to send excessive information from your backend to your frontend. 从后端向前端发送过多的信息是不好的。 If you're going to send data to your frontend from your back-end and a lot of it isn't going to be used, then it's probably best to adjust your backend so that it only returns information that's going to be actually used by your frontend. 如果您打算将数据从后端发送到您的前端,而其中的许多数据将不被使用,那么最好调整一下后端,使其仅返回将被实际使用的信息。您的前端。

Alternatively, if your frontend isn't going to use all the the information sent by your backend right away, but potentially might use it later (based on user input), then it's better to send all the data from your backend and process it on the front end as needed to avoid making future requests to your backend. 另外,如果您的前端不打算立即使用后端发送的所有信息,但有可能稍后再使用(根据用户输入),那么最好从后端发送所有数据并对其进行处理前端,以免将来向后端发出请求。

Taking an array of links as an example: 以链接数组为例:

If the user requests to see a link based on certain criteria, and that's the only link that they are going to see (based on the design of your application), then your backend should process that request and return only the link that your user wants to see to be displayed on the front end. 如果用户根据特定条件请求查看链接,而这是他们唯一要看到的链接(基于应用程序的设计),则后端应处理该请求并仅返回用户想要的链接看到要显示在前端。

If the user can request to see a link, but could potentially request to see another link later, then your backend should send a full array of links that might need to be displayed at some point. 如果用户可以请求查看链接,但是以后可能会请求查看另一个链接,则您的后端应该发送可能需要在某个时候显示的完整链接列表。 Then your frontend can display the links at the appropriate time without having to make a request to your backend each time the user wants to see a new link. 然后,您的前端可以在适当的时间显示链接,而无需每次用户希望查看新链接时都向您的后端发出请求。

In my opinion, if the logic doesn't need to be done by the browser, then do it on the server. 我认为,如果逻辑不需要浏览器完成,则可以在服务器上完成。 It will help you with reducing the size of your app in the long run. 从长远来看,它将帮助您减小应用程序的大小。 You want your final, bundled .js file to be as small as possible. 您希望最终捆绑的.js文件尽可能小。 That's just one small step you can take to contribute to that. 这只是您可以为此做出贡献的一小步。

The short answer is that it all depends on your business logic. 简短的答案是,这完全取决于您的业务逻辑。 Regarding how best to handle an array of items to be sent from backend to front-end, if a user will only ever need to see this one item, for example, then by all means, have the backend parse the array of data on its end and send that single item to the client front-end. 关于如何最好地处理从后端发送到前端的项目数组,例如,如果用户只需要查看该项目,则一定要让后端在后端解析其数据数组。并将该单个项目发送到客户端前端。 If, on the other hand, you anticipate that you'll need to work with an array of items to be presented to the user at some point in the app, it would be reasonable to simply have the backend send the array of items. 另一方面,如果您预计需要在应用程序中的某个位置处理一系列要呈现给用户的项目,那么让后端简单地发送一系列项目是合理的。 Furthermore, that array of items could be, for instance, a filtered version of the items that would be relevant to this particular user. 此外,该项目阵列可以是例如与该特定用户相关的项目的过滤版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM