11

If I use Axios and Superagent to make a call to the same api one after another I get Superagent's response first in the console logs in both cases i.e if I call one first than the other and vice versa. Does that mean one is faster than the other or is something else entirely?

getUser() {

  axios.get('/api/getuser')
    .then((res) => {
      console.log(err,res)          
    })
    .catch((err,res) => {
      console.log(err,res)          
    })

    request
        .get('api/getuser')
        .end((err, res) => {
          console.log(err,res)              
        });
  }
xtabbas
  • 627
  • 4
  • 10
  • 18

2 Answers2

18

The difference is unlikely to be related to the raw speed of the client. Both use Node’s HTTP library or the browser’s built-in XMLHttpRequest. Most likely what you’ve observed are slight differences in timing related to event handling.

I’d base my decision on other factors, like which API you like better, and library size (for a browser-side application).

Here’s a browser-side test case for Axios and SuperAgent: https://jsperf.com/axios-vs-superagent/ and here’s a server-side test: https://gist.github.com/natesilva/24597d954f392b21467b83403756f121

For me, on these tests, Axios is faster in the browser and SuperAgent is faster under Node.js.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Nate
  • 18,752
  • 8
  • 48
  • 54
  • 1
    Sadly the first link https://jsperf.com/axios-vs-superagent/ is no more available. Have you a solution for this? Another source? – johannchopin Apr 23 '20 at 10:19
1

I don't really think that it is really speed thing, well sort of. I think it is that Axios is promise based and Superagent is not. They are both asynchronous but since axios waits to resolve the response it gets it appears "slower" than superagent.

I could be wrong though.

  • 1
    Adding promise support for superagent is erm.. super-easy, there's a plugin - https://github.com/jomaxx/superagent-promise-plugin – monkeyboy Dec 31 '16 at 14:47
  • 1
    of course it is easy, there is also superagent-as-promised, [link]https://www.npmjs.com/package/superagent it is just that op asked why they are coming at different times regardless of order. I believe that I answered the question. However, as I noted I could be wrong – thomcchester Jan 06 '17 at 16:29
  • 11
    Currently, SuperAgent supports Promises out-of-the-box : https://visionmedia.github.io/superagent/#promise-and-generator-support – CedX Jul 27 '17 at 12:20