过滤器逻辑应该在前端还是后端? [英] Filters logic should be on frontend or backend?

查看:26
本文介绍了过滤器逻辑应该在前端还是后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网络应用程序

I’m creating a web application

前端 - reactjs 和后端 java.

Frontend - reactjs and backend java.

前端和后端通过休息相互通信.

Frontend and backend communicate with each other via rest.

在 UI 上,我显示了一个项目列表.我需要过滤它们以获取一些参数.

On UI I show a list of items. And I need to filter them for some params.

选项 1:过滤器逻辑在前端

在这种情况下,我只需要对后端进行 get 调用并获取所有项目.用户选择一些过滤选项后,ui 上会发生过滤.

In this case I just need to make a get call to backend and get all items. After user choose some filter option filtering is happening on ui.

优点:为此我不需要将数据发送到后端并等待响应.刷新列表的速度应该更快.

Pros: for that I don’t need to send data to back end and wait for response. Speed of refreshing the list should be faster.

缺点:如果我需要多个前端客户端.假设一个移动应用程序.比我也需要在这个应用程序上再次创建过滤器.

Cons: If I will need multiple frontend clients. Let’s say a mobile app. Than I need to create filters again on this app too.

选项 2:过滤器逻辑在后端

在这种情况下,我会在加载应用程序时获取所有列表项.用户更改过滤器选项后,我需要发送带有过滤器参数的获取请求并等待响应.之后更新 UI 上的项目列表.

In this case I get all list items when app is loading. After user changes the filter options I need to send a get request with filters params and wait for response. After that update a list of items on UI.

优点:过滤器逻辑只编写一次.

Pros: filter logic is written only once.

缺点:速度可能会慢很多.因为发送请求并返回结果需要时间.

Cons: Speed probably will be much slower. Because it takes time to send request and get a result back.

问题:过滤器逻辑应该在哪里?在前端还是在后端?或者也许什么是最佳实践?

Question: Where the filter logic should be? In frontend or in backend? Or maybe what is a best practice?

推荐答案

在后端进行过滤和限制.如果您有 100 万条记录,并且有 10 万用户试图同时访问这些记录,您真的想向每个用户发送 100 万条记录吗?它会破坏您的服务器和用户体验(等待从后端为每个用户传播 100 万条记录,然后在前端传播,与仅获取 20-100 条记录然后单击(分页)相比需要很长时间按钮检索下一个 20-100).最重要的是,在前端过滤一百万条记录同样需要很长时间,最终也不太实用.

Filter and limit on the back end. If you had a million records, and a hundred thousand users trying to access those records at the same time, would you really want to send a million records to EVERY user? It'd kill your server and user experience (waiting for a million records to propagate from the back end for every user AND then propagate on the front end would take ages when compared to just getting 20-100 records and then clicking a (pagination) button to retrieve the next 20-100). On top of that, then to filter a million records on the front-end would, again, take a very long time and ultimately not be very practical.

从现实世界的角度来看,大多数网站都有某种记录限制:Ebay = 50-200 条记录,Amazon = ~20,Target = ~20...等等.这确保了快速的服务器响应和流畅的用户体验对于每个用户.

From a real world stand point, most websites have some sort of record limit: Ebay = 50-200 records, Amazon = ~20, Target = ~20... etc. This ensures quick server responses and a smooth user experience for every user.

这篇关于过滤器逻辑应该在前端还是后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆