<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://www.googleapis.com/discovery/v1/apis');
xhr.onload = () => JSON.parse(xhr.responseText);
xhr.send();
fetch('https://www.googleapis.com/discovery/v1/apis')
.then(response => response.json());
axios.get('https://www.googleapis.com/discovery/v1/apis')
.then((response) => response.json());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
xhr | |
fetch | |
axios |
Test name | Executions per second |
---|---|
xhr | 12596.5 Ops/sec |
fetch | 11398.0 Ops/sec |
axios | 8784.7 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided benchmark compares three different approaches to make an HTTP GET request: XMLHttpRequest (XHR), fetch, and Axios. The goal is to determine which approach is the fastest.
Options Compared
Pros and Cons
Library Used
Special JS Feature/Syntax
None mentioned in the provided benchmark. However, it's worth noting that fetch uses modern JavaScript features like async/await, which are supported by most modern browsers but may not work in older environments.
Other Alternatives
Benchmark Preparation
The provided benchmark preparation code includes a script tag that loads the Axios library. This is necessary because the benchmark tests each request method individually, requiring them to have access to their respective libraries.
When preparing the benchmark, you would need to create separate scripts for each request method (XHR, fetch, and Axios) and configure the test environment accordingly. The Script Preparation Code
field in the benchmark definition includes the Axios script tag.