承诺拒绝在Node.js< = 18
只需在node.js< = 18:
test.js
function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}
async function test() {
for (let i = 0; i < 1000000; i++) {
await new Promise((resolve, reject) => {
reject('value')
})
.then(() => {}, () => {})
// if (i % 10000 === 0) {
// await delay(100)
// }
}
console.log('OK')
const time0 = Date.now()
await delay(0)
console.log('Real delay time: ' + (Date.now() - time0))
}
test()
中运行它。测试功能将产生100万个承诺拒绝。结束后,它将悬挂。似乎垃圾收集在空闲时召唤,这需要很长时间。 如果将拒绝
用Resolve
替换,那么一切都会快速工作。 您也可以定期延迟删除,并且会迅速工作。不幸的是,即使是此骇客也无济于事。拒绝呼叫逐渐减慢了数十次。
有人知道绕过这个问题的另一种方法吗?
ps:我正在编写一个负载测试,该测试检查模块与承诺拒绝的多种方式。约150万个变体。我可以“减少变体的数量。我还计划在将来编写更多类似的测试,我希望它们工作
Just run it in the Node.JS <= 18:
test.js
function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}
async function test() {
for (let i = 0; i < 1000000; i++) {
await new Promise((resolve, reject) => {
reject('value')
})
.then(() => {}, () => {})
// if (i % 10000 === 0) {
// await delay(100)
// }
}
console.log('OK')
const time0 = Date.now()
await delay(0)
console.log('Real delay time: ' + (Date.now() - time0))
}
test()
The test function will generate 1 million of Promise rejections. And after finish it will hang. It seems like the garbage collection called on idle, and it takes very long time.
If you replace the reject
with the resolve
, then everything will work quickly.
You can also uncomment the periodically delay, and it will work quickly. Unfortunately even this hack does not help on some browsers. Reject calls are gradually slowed down by dozens of times.
Does anyone know another way to bypass this problem?
PS: I'm writing a load test that checks many ways how a module works with Promise rejects. About 1.5 million variants. I can"t reduce the number of variants. I also plan to write many more similar tests in the future and I would like them to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案:
然后使用此解决方案
recount
:I found the solution:
and then use this instead
reject
: