简体   繁体   English

服务器端延迟加载 vs. 客户端延迟加载

[英]Server-side lazy loading vs. Client-side lazy loading

I've been doing some research about infinite scrolling and came across what people call "Lazy Loading".我一直在做一些关于无限滚动的研究,并遇到了人们所说的“延迟加载”。 Now I have done it on one of my website elements (chatbox), and now I have 2 ways to do it.现在我已经在我的网站元素之一(聊天框)上完成了它,现在我有两种方法可以做到。 but I can't decide which one is more efficient than the other.但我无法决定哪一个比另一个更有效率。 Here are my ways:以下是我的方法:

Let's say I have 1,000,000 rows of data from database which I need to fetch all假设我有 1,000,000 行数据需要从数据库中获取

1st way :第一种方式
Load content from database, truncate it on the server-side code(PHP) then only show the first 50. Upon user scroll on the page, another request will be sent to fetch the results again and display the next 50 and so on and so forth..从数据库加载内容,在服务器端代码(PHP)上截断它,然后只显示前 50 个。当用户滚动页面时,将发送另一个请求以再次获取结果并显示下一个 50,依此类推前进..

2nd way :第二种方式
Load content from database, render it in my HTML as hidden elements but only displaying the first 50, then upon user scroll, show 50 more hidden elements.从数据库加载内容,在我的 HTML 中将其呈现为隐藏元素,但只显示前 50 个,然后在用户滚动时显示另外 50 个隐藏元素。

The 1st way is requesting from the server whenever the need to display more results arises.第一种方法是在需要显示更多结果时从服务器请求。 The 2nd way just does 1 request from the server, then hides the result except for the first few that should be shown.第二种方法只从服务器执行 1 个请求,然后隐藏除应显示的前几个之外的结果。

I have no problem doing either of the two.我做这两个都没有问题。 Now the dilemma, is that the 1st way is sending more HTTP requests, and the 2nd way (though sending only 1 HTTP request) is fetching huge data in a single request, which can be slow.现在的困境是,第一种方式是发送更多的 HTTP 请求,而第二种方式(虽然只发送 1 个 HTTP 请求)在单个请求中获取大量数据,这可能很慢。

Which method is "better", and if there are other options, please let me know.哪种方法“更好”,如果还有其他选择,请告诉我。

I know this is old question but want to give my opinion:我知道这是个老问题,但想发表一下我的意见:

1st way第一种方式

Is always preferred specially with that amount of rows, it is much more efficient to only request a set number of rows and if user wants to see more eg click in next page another request is made to the server to fetch the next page, the response time will be much better, it will also make it easier to the client to manipulate the list if there is other processing that needs to be done before is returned to the user.总是特别喜欢这种数量的行,只请求一定数量的行会更有效率,如果用户想要看到更多,例如点击下一页,另一个请求会向服务器发出以获取下一页,响应时间会好很多,如果在返回给用户之前还有其他需要完成的处理,它也会使客户端更容易操作列表。 You also need to make sure that you are applying the limits in your DB query otherwise you will be loading all the objects into memory which is not efficient.您还需要确保在您的数据库查询中应用限制,否则您会将所有对象加载到内存中,这是效率不高的。

2nd way第二种方式

If you fetch 1,000,000 rows at once the user will have to wait until the response comes back which can result in a bad user experience also as the number of rows returned keeps growing the response time will keep increasing and you can hit a time-out eventually, also consider that you will be loading all those objects into memory in your server before is returned.如果您一次获取 1,000,000 行,用户将不得不等到响应返回,这可能会导致糟糕的用户体验,因为返回的行数不断增加,响应时间将不断增加,最终您可能会遇到超时,还要考虑在返回之前您将所有这些对象加载到服务器的内存中。 The only use case I see for this approach is if you have a list that doesn't grow over time or that you have a set number of items that doesn't affect response time.我看到的这种方法的唯一用例是,如果您的列表不会随着时间的推移而增长,或者您有一组不影响响应时间的项目。

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

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