Ajax-intensive page: reuse the same XMLHttpRequest object or create new one every time?
Asked Answered
O

1

6

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. Create one XMLHttpRequest object and reuse that for every HTTP request

  2. Create a new XMLHttpRequest object for every HTTP request

  3. 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

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.

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?

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?)

Onanism answered 18/6, 2012 at 8:50 Comment(3)
Just wondering what server you plan on using and what internet you have hooked up to this because this script if you are correct with the thousands of requests is extremely resource usingTieshatieup
I've got a brutal dedicated server available for this. But thousands of requests are only expected to be performed over the course of several hours, it's not like a request every second or so. More like a few every minute, but it may keep on doing that for many hours.Onanism
ah well javascript will do fine if you needed even faster ie a request per second I would suggest using flash or a java appletTieshatieup
M
3

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

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.

Marriott answered 18/6, 2012 at 8:52 Comment(5)
Thanks, I will go for the easy way (simply creating a new object every time) for now, and look into WebSockets as well. Just to be sure, you're referring to the new HTML5 javascript websocket thing, right?Onanism
Yes, and possibly a library such as socket.io to handle the XHR fallback automatically if necessary.Marriott
Ah, never heard of that before, thanks! One thing though, in the code examples on socket.io it seems to use JavaScript code server-side? Who or what is supposed to execute that code?Onanism
I've written the hll (high level logic) websocket server, which means I can use websockets any time I want any way I want. (smiley-face) I'm not using them yet to connect to browsers because Microsoft is holding up the show. They don't support it till MSIE v10 (and possibly not well yet) ... which, last time I checked, you can only get with Windows 8. But I am looking forward to it.Dehart
Can you elaborate on the overhead? Do you mean network bandwidth, latency, RAM, CPU...?Grab

© 2022 - 2024 — McMap. All rights reserved.