Why should I reuse XmlHttpRequest objects?
- by Xavi
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:
Fewer objects created means less garbage collecting
Reusing XHR objects reduces the chance of memory leaks
The overhead of creating a new XHR object is high
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.