阿贾克斯密集的页面:重复使用相同的XMLHtt prequest对象或创建新的每一次? [英] Ajax-intensive page: reuse the same XMLHttpRequest object or create new one every time?

查看:128
本文介绍了阿贾克斯密集的页面:重复使用相同的XMLHtt prequest对象或创建新的每一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的某种Ajax请求的在线多用户编辑/合作接口,这将是做了很多(如,千)在一个页面生命周期。

I'm working on some sort of online multiuser editor / coop interface, which will be doing a lot (as in, thousands) of ajax requests during one page lifetime.

什么是最好的:(在稳定性,兼容性,避免了麻烦的方面最佳)

What would be best: ('best' in terms of stability, compatibility, avoiding trouble)

  1. 创建一个XMLHtt prequest对象和重用,对每个HTTP请求

  1. Create one XMLHttpRequest object and reuse that for every HTTP request

创建每个HTTP请求新XMLHtt prequest对象

Create a new XMLHttpRequest object for every HTTP request

管理XMLHtt prequest对象的动态池,开始时的HTTP请求,并没有现成的对象是可用的,并且标注pviously创建的对象为可用时,$ P $创建一个新的它的最后一个请求已成功完成

Manage a dynamic 'pool' of XMLHttpRequest objects, creating a new one when starting a HTTP request and no existing object is available, and tagging a previously created object as 'available' when its last request was completed successfully

我觉得1是不是一种选择,导致某些请求可能会失败,我可能会提出新的要求而previous一个还没有完成,等等。

I think 1 is not an option, cause some requests may fail, I may be initiating new requests while a previous one is not finished yet, etc.

至于2,我想这是一个内存泄漏,或者可能会导致疯狂的内存/资源的使用。或者,我可以以某种方式接近或当其请求完成后删除对象? (其中/怎么样?)抑或是JS垃圾收集妥善照顾这本身?

As for 2, I guess this is a memory leak, or may result in insane memory/resource usage. Or can I somehow close or delete an object when its request is finished? (where/how?) Or does the JS garbage collector properly take care of this itself?

没试过3之前,但感觉就像两全其美。或者是这样的不必要的方法,还是我仍然失踪潜在的问题?究竟什么时候可以假设一个请求完成(因此,作为可用于新请求的对象),是接受的readyState 4和HTTP状态200什么时候? (即我可以肯定没有更多的更新或回调将永远遵循之后呢?)

Never tried 3 before but it feels like the best of both worlds. Or is an approach like that unnecessary, or am I still missing potential problems? Exactly when can I assume a request to be finished (thus, the object being available for a new request), is that when receiving readyState 4 and http status 200 ? (i.e. can I be sure no more updates or callbacks will ever follow after that?)

推荐答案

创建一个新的,当你需要一个。 GC将处理旧的,一旦他们不再需要的。

Create a new one when you need one. The GC will deal with the old ones once they are not needed anymore.

然而,这样的事情了合作编辑器,你可能要考虑使用发送请求所有的时间,而不是WebSockets的。一个小的HTTP请求的开销是巨大的,而几乎不存在了WebSocket连接的开销。

However, for something like a cooperative editor you might want to consider using WebSockets instead of sending requests all the time. The overhead of a small HTTP request is huge while there is almost no overhead with a WebSocket connection.

这篇关于阿贾克斯密集的页面:重复使用相同的XMLHtt prequest对象或创建新的每一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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