<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
function showResult(data) {
console.log(data);
}
var newUrl = "https://www.googleapis.com/discovery/v1/apis";
var request = jQuery.ajax({
type: "GET",
url: newUrl,
success: showResult,
data: null
});
fetch(newUrl)
.then(
function(response) {
response.json().then(function(data) {
showResult(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Ajax | |
Fetch |
Test name | Executions per second |
---|---|
Ajax | 10521.3 Ops/sec |
Fetch | 8323.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Overview
The benchmark compares the performance of two approaches: making an AJAX call using jQuery and making a fetch request to retrieve data from Google's API.
Script Preparation Code
The script preparation code is responsible for setting up the environment before running the benchmark. In this case, it defines:
showResult
that logs the received data to the console.newUrl
used in both AJAX and fetch requests.HTML Preparation Code
The HTML preparation code includes a script tag that loads the jQuery library, which is used for making the AJAX call.
Individual Test Cases
There are two test cases:
newUrl
. The jQuery.ajax
method sends a GET request with the specified URL, success callback (showResult
), and no data.newUrl
. It sets up a promise chain to handle both successful and error responses.Comparison
The benchmark is comparing the performance of these two approaches:
Pros and Cons
Here are some pros and cons of each approach:
Pros:
Cons:
Pros:
Cons:
Library: jQuery
jQuery is a popular JavaScript library used for making AJAX calls. It provides an easy-to-use API for sending HTTP requests, manipulating the DOM, and handling events.
In this benchmark, jQuery is used to make an AJAX call to Google's API. The jQuery.ajax
method sends a GET request with the specified URL and success callback (showResult
). This approach uses the browser's native support for XMLHTTPRequest objects under the hood.
Special JS Feature/Syntax: None
There are no special JavaScript features or syntax used in this benchmark, such as async/await or Promises.
Other Alternatives
If you're looking for alternatives to jQuery and the fetch API, consider:
Note that these alternatives may require adjustments to your codebase and workflow.