- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经是 Node.js 开发人员一年了。昨晚我想我会在 express 和 http 模块之间做一个基准测试,基本上它是一个简单的 promise,它返回一个字符串并将它传递给响应,现在我看到 http 更快,但我遇到了一个不同的问题, 如果我在 ab 测试中将 setTimeout
设置为 50 ms,并发 500 和 100000 个请求,响应时间比 setTimeout
0 或 process 快两倍.nextTick
。
现在我知道 setTimeout
将它带到下一个周期,但在队列的末尾, nextTick
将它放在下一个周期的首位,但我真的不理解为什么 setTimeout
50ms 比 setTimeout
0 快。即使没有 setTimeout,ab 测试也比 setTimeout 50ms 慢很多。
我怀疑它与 apache ab 测试有关,或者我错过了 Node 的某些东西?
http.createServer((req,res)=>{
setTimeout(()=>{
check().then(data=>{
res.write(data);
res.end();
})
},0)
}).listen(3000);
let check = () =>{
return Promise.try(()=>{
return 'done with Async'
})
};
// setTimeout 0 times with ab test
Concurrency Level: 500
Time taken for tests: 53.824 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 9000000 bytes
HTML transferred: 1500000 bytes
Requests per second: 1857.90 [#/sec] (mean)
Time per request: 269.121 [ms] (mean)
Time per request: 0.538 [ms] (mean, across all concurrent requests)
Transfer rate: 163.29 [Kbytes/sec] received
// setTimeout 50ms response times
Concurrency Level: 500
Time taken for tests: 23.174 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 9000000 bytes
HTML transferred: 1500000 bytes
Requests per second: 4315.12 [#/sec] (mean)
Time per request: 115.872 [ms] (mean)
Time per request: 0.232 [ms] (mean, across all concurrent requests)
Transfer rate: 379.26 [Kbytes/sec] received
最佳答案
当您使用 setTimeout(0) 时,您在进程中添加了一个额外的作业,这可能需要一些时间。但是使用 setTimeout(50),请求会在队列中准备好,并且在 50 毫秒后,它们将被完全发送出去,而不需要额外的时间。例如,考虑一个请求需要 10 毫秒发送和 10 毫秒获得响应。通过使用 setTimeout(50),发送 500 个请求将花费 5000ms 加上 50ms 超时和 10ms 响应,它将花费 5060ms。现在,如果我们使用 setTimeout(0),我们将有 10ms 用于发送请求,10ms 用于获取响应。因此,对于 500 个请求,它将花费 10000 毫秒。
关于javascript - Node js,为什么50ms的setTimeout比setTimeout 0快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41666855/
要求:用户连续扫描文本框中的作业编号,没有任何延迟。对于每个职位编号,我需要在后台调用 API 来获取扫描职位编号的详细信息。 我做了什么:我编写了一个小模拟代码来激发这个需求。我使用 setTime
我遇到了一个问题:该代码应该按该顺序输出“hi1”“hi2”“hi3”“hi4”。我写了这个简化的代码,实际代码更复杂,导致我无法删除我标记的一些功能。 function test() { c
我的页面上有一个动态创建的 iframe。像这样: var iframe = document.createElement('iframe'); iframe.setAttribute("id","m
我确信这是一个被问过很多次的通用问题,但找不到解决方案。 我有 javascript 使用 setTimeout() 函数来关闭我在设定时间后创建的弹出窗口。 问题:如果我在与创建弹出窗口的脚本相同的
我想在第一个函数完成后执行第二个函数。 结果: i: 0,i: 1,...,i: 9, j: 0,j: 1,...,j: 9 function first(callback){ for
我正在尝试创建一个按钮,以反馈其正在执行的操作。在 Angular 中,我向服务器发出一个放置请求——此时我更改按钮的状态以指示这一点——然后当我收到响应时,我再次更改按钮的状态以反射(reflect
我正在尝试制作一个字符串,它会逐个字母地写出自己直到完成句子,并且每个字母出现的速度基于从 1 到 10 不等的输入。在字符串的末尾,它会闪烁5 秒,直到外星人出现。我的想法是创建一个 setInte
在 Meteor 中,为什么要使用 Meteor.setTimeout() 而不是普通的 setTimeout()? 使用 Meteor.setTimeout() 而不是单纯的 setTimeout
我有这个代码 - function example() { var i = 0; function add() { i++; } setTimeout(
我想知道它们之间有什么区别 window.setTimeout(myFancyFunciton, 1000); 和 setTimeout(myFancyFunciton, 1000); 两者似乎都在做
好吧,我好像遇到了问题。我正在尝试创建一个twicker 来显示数据行。我正在使用 jquery/javascript 在一定时间后隐藏和显示行。代码如下: var timer_is_on
编辑:我最终想在以后使用 setTimeout 恢复变量的先前值 我创建了以下示例来说明我的观点:( JSFiddle ) Push the button Try it var x = {};
我一直在像这样在没有窗口父级的情况下使用超时: setTimeout(FUNC, 1000); 我很好奇,我应该这样使用它吗? window.setTimeout(FUNC, 1000); 有区别吗?
我有一个使用 setTimeout 函数执行动画的函数,结构如下: animation: function() { //first setTimeout(function(){ mak
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
这是一个快速的(损坏的)jsfiddle:http://jsfiddle.net/wH2qF/ 由于某种原因这不起作用...是因为我在另一个 setTimeout 的处理程序中有一个 setTimeo
我有两个 setTimouts,如下所示,根据 if 条件,我想跳过一个超时。 var batchID = []; batchID = getBatchIDs();//this function ge
我只看到一种情况我应该使用 window.setTimeout 而不是 setTimeout,当我在我的闭包,这显然不是很好的做法(除非有非常特殊的用途)。 我注意到 Google Closure 编
我看到这个用了很多,有人告诉我把函数引用放在引号之间是不好的,因为 setTimeout/setInterval evals 引用。这两者之间的实际区别是什么,以至于一个被使用在另一个之上?为什么我看
我正在使用“setTimeout”函数。此代码按预期运行: function myFunction() { console.log('test'); setTimeout(myFunc
我是一名优秀的程序员,十分优秀!