将节点和Chrome的响应时间进行比较,节点较慢。我正在向同一页面提出请求;发出两个请求,一个来自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: ");
})();
铬:转到url,然后在控制台中运行它。
(async () => {
console.time("Load Time: ");
var request = await fetch(location);
console.timeEnd("Load Time: ");
})();
结果:
- 节点:~3.746s
- 铬:1030.53515625ms
我们能做些什么来解决这个问题吗?
谢谢你的帮助。
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的运行