简体   繁体   English

在客户端或服务器端对数据进行操作是最佳实践吗?

[英]Is it a best practice to do operations with data in the client-side or in the server-side?

For example, if I have to sort an array of data from a database to append it on the body of a web page.例如,如果我必须将数据库中的数据数组排序到 append ,则它位于 web 页面的主体上。 In terms of performance, should I sort it on the server-side and then send it to the client or should I send the data as it is and sort it on the client-side or it makes no difference?在性能方面,我应该在服务器端对其进行排序然后将其发送到客户端还是应该按原样发送数据并在客户端对其进行排序还是没有区别?

If you do it on the client, then every client has to sort the data no matter how fast the client computer is.如果您在客户端上执行此操作,那么无论客户端计算机有多快,每个客户端都必须对数据进行排序。 It might be a developer's ultra-powerful laptop or it might be someone's 5-year old budget Android phone.它可能是开发人员的超强笔记本电脑,也可能是某人使用了 5 年的预算 Android 手机。

If you do it on the server, then the server has to sort the data for every client and you have to pay for the computing resources.如果你在服务器上做,那么服务器必须为每个客户端对数据进行排序,你必须为计算资源付费。 However, you do get the opportunity to cache the sorted data which is a saving (if the data doesn't change too frequently).但是,您确实有机会缓存已排序的数据,这是一种节省(如果数据不经常更改)。

There isn't a one-size-fits-all answer here.这里没有一个万能的答案。

This is a common problem that beginner, intermediate, and expert developers may struggle with.这是初学者、中级和专家开发人员可能会遇到的常见问题。 It's really an architectural question that is best answered with as much context as possible.这确实是一个架构问题,最好用尽可能多的上下文来回答。

What is your API running on?你的 API 在什么平台上运行? If you're hosting in the cloud (AWS, Azure, etc.), are you trying to minimize hosting costs by minimizing resource usage?如果您在云中托管(AWS、Azure 等),您是否试图通过最小化资源使用来最小化托管成本? If so, pushing that work onto clients may help at scale, but is probably negligible for small applications.如果是这样,将这项工作推给客户可能会在规模上有所帮助,但对于小型应用程序可能可以忽略不计。

What about your clients?你的客户呢? Are you only supporting desktop machines in an enterprise environment where there's a high probability that you can generalize their specifications?您是否仅在企业环境中支持桌面计算机,您可以将其规范概括的可能性很高? Or are IoT devices with minimal computing resources contacting your API?或者计算资源最少的物联网设备是否与您的 API 联系? In the latter case the client device may not have the memory or computational power to sort the data while still meeting the rest of its duties.在后一种情况下,客户端设备可能没有 memory 或计算能力来对数据进行排序,同时仍满足其职责的 rest。 What about mobile devices that may be 10-20 years old?可能使用 10-20 年的移动设备呢?

Is this fixed behavior on your API or can clients request server-side sorting?这是 API 上的固定行为还是客户端可以请求服务器端排序? What sort of flexibility do you want your API to provide and how feasible is it to implement switches and parameters for every facet?您希望 API 提供什么样的灵活性?在各个方面实现开关和参数的可行性如何? At what point are you doing too much, obscuring the purpose of the API with a plethora of buttons and dials to configure or tune.你在什么时候做得太多,用过多的按钮和拨盘来配置或调整 API 的目的。

In my personal experience, I like ORMs like Sequelize that can expose a lot of flexibility on your API with little work, but these tools can come at a computational cost and potential (but usually infrequent) security issues .以我个人的经验,我喜欢像Sequelize这样的 ORM,它可以通过很少的工作在您的 API 上提供很大的灵活性,但是这些工具可能会带来计算成本和潜在的(但通常不常见的)安全问题 They're very general implementations that aren't always perfect for your use case.它们是非常通用的实现,并不总是适合您的用例。

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

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