<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1=/jquery.min.js"></script>
function showResult(data) {
console.log(data);
}
var newUrl = "https://www.googleapis.com/discovery/v1/apis";
var request = $.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 | 0.0 Ops/sec |
Fetch | 7298.2 Ops/sec |
I'd be happy to help explain the benchmark and its results.
What is being tested?
The provided benchmark compares two approaches: making an AJAX call using jQuery's $.ajax()
method, and making a fetch request to retrieve data from a URL.
Options being compared
There are two options being compared:
$.ajax()
method to make a GET request to the specified URL.Pros and Cons of each approach
AJAX Call
Pros:
Cons:
Fetch Request
Pros:
Cons:
Library used
In this benchmark, jQuery is used for its $.ajax()
method. jQuery is a popular JavaScript library that provides an easy-to-use interface for making AJAX calls.
Special JS feature or syntax
Neither of the two options uses any special JavaScript features or syntax beyond what is required for basic Fetch API and AJAX call functionality.
Other alternatives
If using the Fetch API, alternative approaches include:
XMLHttpRequest
object (although less efficient)For making AJAX calls with jQuery, alternative approaches include:
XMLHttpRequest
object instead of jQuery's $.ajax()
methodOverall, the choice between Fetch API and AJAX calls depends on factors such as performance requirements, browser support, and personal preference. The Fetch API is generally recommended for its simplicity and efficiency.