Fetch vs XMLHttpRequest vs Jquery Ajax

3

Hello. I know that the Fetch method of ES6 is the most recent. But I saw on the JSPerf site that fetch is the slowest compared to the famous XmlHttpRequest of JS Vanilla, and even slower than $.Ajax of JQuery.

Why is it slower if it is the most current? Can someone explain me?

Test link: XHR vs. jQuery vs fetch

Thank you.

    
asked by anonymous 10.01.2018 / 12:50

1 answer

2

The fetch API is actually JS Vanilla because it does not belong to any library of third as the functions of jQuery and intends to solve some problems of XMLHttpRequest (XHR), which reigned for more than a decade in the asynchronous requests in Javascript, as:

  • Input, output and managed states interacting with only one object
  • Event-based state
  • Do not work well with Promise

Font

fetch() differs from jQuery.ajax() mainly in two ways:

    The Promise returned from fetch() will not reject the status of the HTTP error, even if the response is an HTTP 404 or 500. Instead, it will resolve normally, and it will only reject in if anything fails to prevent the request from completing.
  • By default, the search will not send or receive cookies from the server, resulting in unauthenticated requests if the site is based on the maintenance of a user session.

Performance may vary among browsers, as each browser will have its implementation of window.fetch ( eg Google's V8 or Mozilla's Spidermonkey).

Why slow down?

The main reason for the difference in performance is due to fetch() having more options regarding XHR, which we can find here . Doing something new does not necessarily mean that it should be faster, which is what happens in that case - available resources seem to be the priority.

Other sources:

10.01.2018 / 20:53