与使用编程语言相比,为什么浏览器对 API 请求的响应速度要快得多?

2023-12-03

调用 GET 时https://jsonplaceholder.typicode.com/todos/1在浏览器(Chrome v107)中我得到响应时间30ms

另外,当我在 Chrome 开发工具控制台中运行以下代码片段时,时间相同

const start = Date.now();
fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(() => console.log((Date.now() - start) + 'ms'))
  .catch((err) => console.error(err));

但是当我在 NodeJS v18 中运行以下代码片段时(例如axios),我四处走动140ms up to 200ms

import axios from 'axios';

const start = Date.now();
axios
  .get('https://jsonplaceholder.typicode.com/todos/1')
  .then(() => console.log((Date.now() - start) + 'ms'))
  .catch((err) => console.error(err));

我什至用 Rust 编写了以下脚本并解决了130ms up to 160ms

fn main() {
    let start = std::time::Instant::now();
    let resp = reqwest::blocking::get("https://jsonplaceholder.typicode.com/todos/1");
    let duration = start.elapsed();
    println!("Time elapsed is: {:?}", duration);
}

我在 Arch Linux x86_64 5.15.76-1-lts 和 MacOS 13.0 上测试了这个,结果相同


我想知道并了解为什么会这样,以及我是否可以进行任何类型的优化以在 NodeJS 中获得更快的响应时间


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

与使用编程语言相比,为什么浏览器对 API 请求的响应速度要快得多? 的相关文章

随机推荐