简体   繁体   中英

Fastest way to request API with NodeJS

I'm trying to request Binance API https://api.binance.com/api/v3/ping in a NodeJS app in order to make some trades. I'm using axios to request the API

const testBinanceAPIResponseTime = async () => {
  try {
    await axios.get("https://api.binance.com/api/v3/ping");
  } catch (err) {
    throw new Error(err);
  }
};

I added an interceptor to measure response time

axios.interceptors.request.use((x) => {
  x.meta = x.meta || {};
  x.meta.requestStartedAt = new Date().getTime();
  return x;
});

axios.interceptors.response.use((x) => {
  console.log(
    `Execution time for: ${x.config.url} - ${
      new Date().getTime() - x.config.meta.requestStartedAt
    } ms`
  );
  return x;
});

My issue is the response time is always around 220 ms wether I deploy my code on Heroku EU or Heroku US. According to "documentation", requests from US should be a way faster (~ 10 ms ) than EU since there is some Binance servers there. Is there a way to improve that response time by using another lib or another pattern?

Your Data can not travel faster than the speed of the light. So If you have 3000 km distance that is equal to 3 * 10^6 meters and the speed of light is 3 * 10 ^ 8 meter/second timeTaken = distance/velocity which is 3*10^6 / 3*10^8 =10^-2 (.01 s) which is 10 ms (10 * 10 ^ -3). So when you count rount trip time that is sent and recieved it will be double 20 ms that is the fastest you can get. I have not included routing and other times And also apis have their own response time (processing time of your request).If the heroku server is far away from your binance server even though its in america, you can not get the desired speed.

I would start by trying to do a request with Postman for example or a tool to test the real speed of it.

After it, I would not use the axios Npm, use a regular fetch request .

Also, check the speed of your Heroku server(the difference between local and on server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM