<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
const url = "https://jsonplaceholder.typicode.com/todos";
axios.get(url)
.then((response) => console.log(response.data));
const url = "https://jsonplaceholder.typicode.com/todos";
fetch(url)
.then((response) => response.json())
.then((data) => console.log(data));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
axios | |
fetch |
Test name | Executions per second |
---|---|
axios | 14482.4 Ops/sec |
fetch | 23660.1 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. In this case, we have two individual test cases comparing the performance of axios
and fetch
libraries in making HTTP requests.
Benchmark Definition
The benchmark definition consists of a script preparation code and an HTML preparation code. The script preparation code is empty, which means that the user needs to provide their own script for each test case. However, in this example, the provided scripts are:
axios
: const url = "https://jsonplaceholder.typicode.com/todos"; axios.get(url).then((response) => console.log(response.data));
fetch
: const url = "https://jsonplaceholder.typicode.com/todos"; fetch(url) .then((response) => response.json()) .then((data) => console.log(data));
These scripts make a GET request to the same JSONPlaceholder API endpoint, but using different libraries.
Options Compared
The two options being compared are:
axios
: A popular JavaScript library for making HTTP requests.fetch
: A built-in JavaScript API for making HTTP requests.Pros and Cons of Each Approach
axios
:fetch
, with support for things like HTTP headers, query parameters, and caching.fetch
in certain scenarios (e.g., when dealing with large payloads).fetch
:axios
, especially for simple requests.axios
, requiring more manual setup and error handling.axios
.Library: Axios
Axios
is a popular JavaScript library for making HTTP requests. It provides a simple and intuitive API for sending HTTP requests, with features like:
The purpose of using axios
in this benchmark is to compare its performance against the built-in fetch
API.
Special JS Feature: Fetch
The fetch
API is a built-in JavaScript feature that allows making HTTP requests. It provides a simple and modern way of sending HTTP requests, with features like:
The purpose of using fetch
in this benchmark is to compare its performance against the external library axios
.
Other Alternatives
If you're looking for alternative libraries or APIs for making HTTP requests, some options include:
jQuery.ajax()
: A part of the jQuery library, which provides a way of sending HTTP requests with features like caching and error handling.XMLHttpRequest
: A built-in JavaScript API for making HTTP requests, but less feature-rich than fetch
or axios
.node-http
: A lightweight HTTP client library for Node.js, which can be used to make HTTP requests.Overall, the choice of using axios
or fetch
(or another library) depends on your specific use case and performance requirements.