承诺拒绝在Node.js< = 18

发布于 2025-02-09 20:21:13 字数 904 浏览 1 评论 0原文

只需在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2025-02-16 20:21:13

我找到了解决方案:

function promiseRejected(reason?: any): PromiseLike<never> {
  return {
    then(_, reject): any {
      void reject(reason)
    },
  }
}

export function rejectAsResolve(resolve: (value: any) => void, reason?: any) {
  resolve(promiseRejected(reason))
}

然后使用此解决方案recount

new Promise((resolve) => {
  rejectAsResolve(resolve, 'ERROR')
})

I found the solution:

function promiseRejected(reason?: any): PromiseLike<never> {
  return {
    then(_, reject): any {
      void reject(reason)
    },
  }
}

export function rejectAsResolve(resolve: (value: any) => void, reason?: any) {
  resolve(promiseRejected(reason))
}

and then use this instead reject:

new Promise((resolve) => {
  rejectAsResolve(resolve, 'ERROR')
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文