Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
XHR vs Fetch for JSON
(version: 0)
Comparing performance of:
XHR vs Fetch
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
XHR
const xhr = new XMLHttpRequest(); xhr.responseType = 'json'; xhr.open('GET', 'https://www.googleapis.com/discovery/v1/apis'); xhr.onload = () => console.log(xhr.response != null); xhr.send();
Fetch
fetch("https://www.googleapis.com/discovery/v1/apis") .then(resp => resp.json()) .then(data => console.log(data != null));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
XHR
Fetch
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark test cases. **Overview** The benchmark tests two approaches for making HTTP requests: `XMLHttpRequest` (XHR) and the Fetch API. Both methods are used to retrieve data from a URL, in this case, an API endpoint. **Benchmark Definition JSON** The benchmark definition JSON contains three essential fields: * `Name`: The name of the benchmark, which is "XHR vs Fetch for JSON". * `Description`: An empty string, indicating that no description is provided. * `Script Preparation Code` and `Html Preparation Code`: These fields are empty, meaning that no specific code needs to be executed before running the benchmark. **Individual Test Cases** There are two test cases: 1. **XHR (XMLHttpRequest)**: * The benchmark definition is a JavaScript code snippet that creates an XHR object, sets its response type to "json", opens a GET request to the specified API endpoint, and logs the response when it's received. 2. **Fetch**: * The benchmark definition is another JavaScript code snippet that uses the Fetch API to make a GET request to the same API endpoint. It uses the `then` method to parse the response as JSON and log the result. **Library Used** In both cases, no external library is used. Instead, these APIs are built into modern browsers. **Special JS Feature/Syntax (Not applicable in this case)** There are no special JavaScript features or syntax mentioned in either benchmark definition. **Other Alternatives** If you want to compare the performance of other HTTP request libraries or approaches, such as: * `axios` * `jQuery.ajax()` * `fetch` with a library like `reqwest` * A Node.js-based implementation using the `http` module You can create additional benchmark test cases for these alternatives. **Comparison Options** When comparing XHR and Fetch, consider the following factors: * **Performance**: Fetch is generally faster than XHR because it's a native API in modern browsers. * **Error Handling**: Both APIs handle errors differently. XHR uses the `onerror` event to catch errors, while Fetch returns an error object if the response fails. * **Caching**: Fetch has better caching support than XHR, which can result in faster subsequent requests. * **Security**: Fetch is more secure than XHR due to its built-in support for CORS (Cross-Origin Resource Sharing) and its ability to handle errors in a more robust way. **Pros and Cons of Each Approach** XHR: Pros: * Well-established API with good error handling * Works in older browsers that don't support Fetch Cons: * Less efficient than Fetch due to its additional overhead * Not as secure as Fetch due to CORS issues Fetch: Pros: * Faster and more efficient than XHR * More secure due to its built-in CORS support * Better caching capabilities Cons: * Not available in older browsers without polyfills or workarounds * Requires modern JavaScript features (ES6+) I hope this explanation helps you understand the benchmark test case and provides a solid foundation for comparing XHR and Fetch!
Related benchmarks:
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone more complex object
Lodash cloneDeep vs JSON.stringify + JSON.parse
Lodash cloneDeep vs structuredClone vs JSON.stringify with array values
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone (larger object)
Lodash cloneDeep vs structuredClone vs JSON Parse (Big Object)
Comments
Confirm delete:
Do you really want to delete benchmark?