![](/img/trans.png)
[英]Fastest way to serialize and compress a hashmap (object) in nodejs?
[英]Fastest way to request API with NodeJS
我正在嘗試在 NodeJS 應用程序中請求 Binance API https://api.binance.com/api/v3/ping以進行一些交易。 我正在使用 axios 請求 API
const testBinanceAPIResponseTime = async () => {
try {
await axios.get("https://api.binance.com/api/v3/ping");
} catch (err) {
throw new Error(err);
}
};
我添加了一個攔截器來測量響應時間
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;
});
我的問題是響應時間總是在220 ms
左右,無論我在 Heroku EU 或 Heroku US 上部署我的代碼。 根據“文檔”,來自美國的請求應該比歐盟更快(約10 ms
),因為那里有一些 Binance 服務器。 有沒有辦法通過使用另一個庫或其他模式來改善響應時間?
您的數據不能以超過光速的速度傳播。 所以如果你有 3000 公里的距離等於 3 * 10^6 米,光速是 3 * 10 ^ 8 米/秒timeTaken = distance/velocity
是3*10^6 / 3*10^8 =10^-2 (.01 s)
,即 10 毫秒 (10 * 10 ^ -3)。 因此,當您計算發送和接收的往返行程時間時,它將是您可以獲得的最快速度的兩倍 20 毫秒。 我沒有包括路由和其他時間,而且 api 有自己的響應時間(您的請求的處理時間)。如果 heroku 服務器遠離您的幣安服務器,即使它在美國,您也無法獲得所需的速度。
例如,我會嘗試使用Postman
或測試其實際速度的工具來發出請求。
之后,我不會使用axios
Npm,使用常規的fetch request
。
另外,檢查您的 Heroku 服務器的速度(本地和服務器上的區別)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.