Noob
Noob

Reputation: 2927

What is the fastest way to work with ajax request?

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.

Upvotes: 0

Views: 3901

Answers (4)

user1000131
user1000131

Reputation:

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.

Upvotes: 1

Sinetheta
Sinetheta

Reputation: 9429

.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 common settings, thereby creating your own short-cut function.

Upvotes: 4

Pekka
Pekka

Reputation: 449415

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

Upvotes: 6

jlindenbaum
jlindenbaum

Reputation: 1881

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

Upvotes: 2

Related Questions