admin管理员组文章数量:1291203
I am a little bit confused about what is the fastest and the friendly way with the server to request POST or GET from server by AJAX, is it jQuery ($.load()
, $.get()
, $.post()
, $.ajax()
) or Javascript like XMLHttpRequest?
I need to make a function or class to use in my project to call request from server via AJAX, but I don't know what is the faster and more friendly with the server jQuery or Javascript.
I am a little bit confused about what is the fastest and the friendly way with the server to request POST or GET from server by AJAX, is it jQuery ($.load()
, $.get()
, $.post()
, $.ajax()
) or Javascript like XMLHttpRequest?
I need to make a function or class to use in my project to call request from server via AJAX, but I don't know what is the faster and more friendly with the server jQuery or Javascript.
Share Improve this question edited Dec 28, 2011 at 18:46 Andrew Marshall 97k20 gold badges227 silver badges217 bronze badges asked Dec 28, 2011 at 18:40 NoobNoob 2,9276 gold badges36 silver badges48 bronze badges 1-
.load
,.get
and.post
are all just shorthand for.ajax
which is just a wrapper forXMLHttpRequest
. So, they're all the same. – gen_Eric Commented Dec 28, 2011 at 18:43
4 Answers
Reset to default 6is it jquery $.load() , $.get() , $.post() , $.ajax()
Those functions are all quasi-synonyms of each other; jQuery adds some syntactic sugar and a standardized interface, but in the end, it uses the browser's native Ajax engine (like XMLHttpRequest
). I don't think there are notable performance differences between any of them.
There may be super-intensive edge cases with thousands of requests where using the "raw" browser engine could shave off a few milliseconds here or there, but for the majority of cases, it won't matter.
You can most likely use jQuery's functions (or those of another library if you need to) without even thinking about performance for a long, long while. A lot of big names use jQuery heavily, including Stack Overflow for their chat.
.load()
, $.get()
, $.post()
and $.getJSON()
for that matter, are all just short-cuts for $.ajax()
. They are just $.ajax()
with some of the settings pre-determined for you.
The only thing unique about .load()
is that it already has a success handler defined for you which injects the retrieved content into your current target.
They are all equally fast, the only question is which you find most readable.
If you will be making numerous similar ajax calls then it is best to use $.ajaxSetup() in an accessible place (read: near top). This allows you to pre-set your most mon settings, thereby creating your own short-cut function.
jQuery just wraps XMLHttpRequest and standardizes the interface for you across all browsers. There's a little overhead, but none on the network side of things. I'd go as far as saying that the overhead of jQuery's functions is trivial when paring it to the amount of time you're going to save yourself having to standardize the XMLHttpRequest across browsers yourself.
Otherwise your speed is determined by the server's bandwidth, the clients connecting to your server, amount of traffic, speed/optimization of backend code etc. etc.
jQuery is javascript. All the ajax functions you described rely on XMLHttpRequest (or its alternatives in older browsers), so they will be slower than using the browser's functionality directly.
The tradeoff is, of course, that jQuery's functions are easier to use. So, if it's efficiency that is your first concern, write your own ajax function (or use ajax directly). If you just want to get it done then using jQuery will be quicker.
本文标签: javascriptWhat is the fastest way to work with ajax requestStack Overflow
版权声明:本文标题:javascript - What is the fastest way to work with ajax request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507471a2382405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论