javascript - Why should I reuse XmlHttpRequest objects? - Stack Overflow

From what I understand, it's a best practice to reuse XmlHttpRequest objects whenever possible.U

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 that by trying to reuse XHR objects you increase your code plexity and you introduce possible browser inpatibilities. So why do so many people remend it?

After some research, I was able to e up with this list of possible explanations:

  1. Fewer objects created means less garbage collection
  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 still a bit skeptical. Are any of these reasons actually valid? If not, what is a valid reason?

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 that by trying to reuse XHR objects you increase your code plexity and you introduce possible browser inpatibilities. So why do so many people remend it?

After some research, I was able to e up with this list of possible explanations:

  1. Fewer objects created means less garbage collection
  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 still a bit skeptical. Are any of these reasons actually valid? If not, what is a valid reason?

Share Improve this question edited Apr 21, 2010 at 22:55 Xavi asked Apr 21, 2010 at 6:24 XaviXavi 20.5k14 gold badges74 silver badges64 bronze badges 1
  • 3 stackoverflow./questions/11502244/… seems to disagree. See also stackoverflow./questions/11079543/… – nafg Commented Jun 5, 2013 at 6:47
Add a ment  | 

1 Answer 1

Reset to default 12

There are a whole host of problems relating to the number of open connections you can have at any one time; often this is imposed at a browser level as in all versions of Internet Explorer (IE6 allows 2, IE7 allows 2, IE8 allows 4), often this is imposed by server throttling and sometimes this is imposed by Internet Service Providers.

If you have a large number of distinct XmlHttpRequest objects in one script, and for some reason some of their connections have not closed properly or have not been GC'd, you may run into difficulty opening new connections and have absolutely no idea what is going wrong.

That and all of the reasons you mention.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743656338a4485437.html

相关推荐

  • javascript - Why should I reuse XmlHttpRequest objects? - Stack Overflow

    From what I understand, it's a best practice to reuse XmlHttpRequest objects whenever possible.U

    4天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信