【问题标题】:Chrome vs Node - Why node is fetching slower than Chrome?Chrome vs Node - 为什么节点的获取速度比 Chrome 慢?
【发布时间】:2020-08-19 03:05:49
【问题描述】:

比较 NodeChrome 的响应时间,node 较慢。我正在向同一页面发出请求;发出两个请求,一个来自 Node,另一个来自 Chrome 控制台。

  • Chrome v:最新版
  • 节点 v:12
  • 操作系统:Windows 64x

节点:

const fetch = require("node-fetch");

const url =
  "https://poshmark.com/search?query=t%20shirts&availability=sold_out&department=All";
(async () => {
  console.time("Load Time: ");
  await fetch(url);
  console.timeEnd("Load Time: ");
})();

Chrome: 转到 url,然后在控制台中运行它。

  (async () => {
    console.time("Load Time: ");
    var request = await fetch(location);
    console.timeEnd("Load Time: ");
})();

结果:

  • 节点:~3.746s
  • Chrome:1030.53515625ms

有什么办法可以解决这个问题吗?

感谢您的帮助。

【问题讨论】:

  • 您是否 100% 确定 fetch() 确实阅读了响应的正文?如果你不调用res.json()res.text() 或类似的东西并等待它完成,那么你还没有阅读fetch() 的响应正文,只是收到了标题。
  • 我担心的是实际请求完成时间没有响应正文。如果您在网络选项卡中检查此请求并移至计时部分,您将能够看到完成请求所花费的总时间。
  • 您为fetch() 显示的代码不会读取数据主体,因此这不是一个公平的比较。如果您想说明更完整的代码,请显示。我们需要准确了解您在比较的内容。
  • @jfriend00 - 你说得对,我现在添加了 fetch 与 node fetch 比较,两者的代码几乎相同,但仍然得到不同的结果。
  • 您实际上想测量什么?获取标题的时间?或者是获得完整响应的时间?

标签: node.js performance google-chrome http axios


【解决方案1】:

使用 node.js 中的这段代码,使用两个不同的库加载整个响应:

"use strict";

const got = require('got');
const fetch = require("node-fetch");


async function run1(silent) {
    let url = "https://poshmark.com/search?query=t%20shirts&availability=sold_out&department=All";
    url += `&random=${Math.floor(Math.random() * 1000000)}`;
    if (silent) {
        let {body} = await got(url);
    } else {
        console.time("Load Time Got");
        let {body} = await got(url);
        console.timeEnd("Load Time Got");
    }
}



async function run2(silent) {
    let url = "https://poshmark.com/search?query=t%20shirts&availability=sold_out&department=All";
    url += `&random=${Math.floor(Math.random() * 1000000)}`;
    if (silent) {
        let body = await fetch(url).then(res => res.text());
    } else {
        console.time("Load Time Fetch");
        let body = await fetch(url).then(res => res.text());
        console.timeEnd("Load Time Fetch");
    }
}

function delay(t) {
    return new Promise(resolve => setTimeout(resolve, t));
}

async function go() {
    await delay(1000);
    // one throw away run for each to make sure everything is fully initialized
    await run1(true);
    await run2(true);

    // let garbage collector settle down
    await delay(1000);

    await run1(false);
    await delay(1000);
    await run2(false);

}

go();

并且,分别运行四次,我得到以下结果:

Load Time Got: : 967.574ms
Load Time Fetch: : 921.211ms

Load Time Got: 872.823ms
Load Time Fetch: 858.379ms

Load Time Got: 802.700ms
Load Time Fetch: 930.276ms

Load Time Got: 819.646ms
Load Time Fetch: 966.878ms

所以,我在这里没有看到多个第二个响应。请注意,我正在为每个 URL 生成一个唯一的查询参数,以尝试在任何地方阻止任何缓存。

在加载整个响应正文的浏览器控制台中使用类似代码中的类似随机 URL,我得到了 748.2ms、647.9ms、738.2ms 的运行时间。

【讨论】:

  • @rehan - 我很好奇为什么没有回应?您对此有何看法?
  • 我需要尝试同样的方法,然后我可以分享我的结果进行比较。感谢您的努力。
  • 我得到了相同的结果——Chrome 大约 900 毫秒,Node JS 大约 2 秒。
  • @Rehan - 好吧,这里无法重现,所以我不知道该说什么。仅供参考,我在 Windows 10 上运行节点 v12.13.1。
  • 我都试过了,最新的和你安装的版本,但是节点端有延迟。
猜你喜欢
  • 1970-01-01
  • 2011-06-28
  • 2014-02-27
  • 1970-01-01
  • 2017-10-27
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多