Why should I reuse XmlHttpRequest objects?

Posted by Xavi on Stack Overflow See other posts from Stack Overflow or by Xavi
Published on 2010-04-21T06:24:51Z Indexed on 2010/04/21 7:13 UTC
Read the original article Hit count: 287

Filed under:
|
|

From what I understand, it's a best practice to reuse XmlHttpRequest objects whenever possible. Unfortunately, I'm having a hard time understanding why. It seems like trying to reuse XHR objects can increase code complexity, introduce possible browser incompatibilities, and lead to other subtle bugs.

After researching this question for a while, I did come up with a list of possible explanations:

  1. Fewer objects created means less garbage collecting
  2. Reusing XHR objects reduces the chance of memory leaks
  3. The overhead of creating a new XHR object is high
  4. The browser is able to perform some sort of network optimization under hood

But I'm not sure if any of these reasons are actually valid.

Any light you can shed on this question would be much appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX