DEV Community

Cover image for Axios or fetch(): Which should you use?

Axios or fetch(): Which should you use?

Brian Neville-O'Neill on September 18, 2019

Written by Faraz Kelhini✏️ In my recent post “How to make HTTP requests like a pro with Axios,” I discussed the benefits of using the Axios librar...
Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

Hmm.

The URL is passed as an argument to fetch(). In Axios, however, the URL is set in the options object

I believe axios has alias methods (which you even used later in the article), ie. axios.get() - github.com/axios/axios#request-met...

Collapse
 
mika76 profile image
Mladen Mihajlović

There’s a nice wrapper for fetch at github.com/elbywan/wretch

Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

Thanks for that :)

Edit: ouch, 7.6KB minified :x
I guess author did a little bit too much for my taste ;)

Collapse
 
brieucp profile image
brieucp

Well, axios is 12.6KB minified.

Thread Thread
 
pavelloz profile image
Paweł Kowalski

Native fetch is 0KB minified. ;)

Thread Thread
 
brieucp profile image
brieucp • Edited

Yes it's true, but this can apply to any library and frameworks. Vanilla is always lighter except for the amount of boilerplate code it can bring. And fetch can bring a tons of boilerplate.

I'll cut 3KB (gzipped) on the framework any day to have a good base for the service layer.

Thread Thread
 
pavelloz profile image
Paweł Kowalski • Edited

I guess priorities differ.

Im not willing to import 12KB of abstractions to use one/two methods of it and i didnt find fetch to bring a lot of boilerpate for me.

Collapse
 
_ezell_ profile image
Ezell Frazier

Axios is my go-to for server-side rendering of applications since it works independently of the window. But there's also isomorphic-fetch, so there's that as well. Haven't had any complaints about either. 🤷‍♂️

Collapse
 
sebbdk profile image
Sebastian Vargr • Edited

If someone suggests me to use a library for something that can be done just fine without it, then i usually shoo them away, usually whilst wearing a robe.

Then i turn around and continue worshiping our great leader, the minimal payload.

youtube.com/watch?v=xJB2xEeq3MA

Collapse
 
sebbdk profile image
Sebastian Vargr • Edited

For reference, Axios is a 5.5kb library, gzipped.

That is absolutely humongous, relatively speaking.

raw.githubusercontent.com/axios/ax...

Collapse
 
edmundcwm profile image
edmundcwm

What about cancelling requests? Am I right to say that axios makes it easy with its cancel token API but fetch doesn’t have any native way to do it?

Collapse
 
simonas88 profile image
simonas88

Now we're asking the real questions

Collapse
 
muhammedmoussa profile image
Moussa

angular user, just HttpClient ;D

Collapse
 
sonicoder profile image
Gábor Soós

Why mutate global objects when you can write a wrapper around it?