Ajax 密集型页面:重用同一个 XMLHttpRequest 对象还是每次都创建一个新对象?

2023-05-15前端开发问题
1

本文介绍了Ajax 密集型页面:重用同一个 XMLHttpRequest 对象还是每次都创建一个新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在开发某种在线多用户编辑器/合作界面,它将在一个页面的生命周期内执行大量(如数千个)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. 创建一个 XMLHttpRequest 对象并为每个 HTTP 请求重用它

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

为每个 HTTP 请求创建一个新的 XMLHttpRequest 对象

Create a new XMLHttpRequest object for every HTTP request

管理 XMLHttpRequest 对象的动态池",在启动 HTTP 请求且没有现有对象可用时创建一个新对象,并在上次请求成功完成时将先前创建的对象标记为可用"

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 不是一个选项,因为某些请求可能会失败,我可能会在前一个请求尚未完成时发起新请求,等等.

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

这篇关于Ajax 密集型页面:重用同一个 XMLHttpRequest 对象还是每次都创建一个新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455