nodejs / nodejs/node

assert.ok: extremely slow error message generation for some code

オープン
#52,677 コメント 11 件 リアクション 1 件 担当者 1 名 GitHub で見る

@avivkeller がすでに取り組んでいます。

2026年8月25日 から。

assert confirmed-bug performance
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

Version

v20.11.1

Platform

Darwin wololobook 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:11:05 PDT 2024; root:xnu-10063.101.17~1/RELEASE_X86_64 x86_64 i386 Darwin

Subsystem

assert

What steps will reproduce the bug?
  1. Create an empty file with thousands of columns, e.g. 900,000 ;'s
  2. Append require('node:assert')(null)
  3. getErrMessage, specifically findColumn feeding into acorn takes several minutes
How often does it reproduce? Is there a required condition?

We've experienced this in production a number of times but we were never able to figure out what the problem was. We ship minified code to AWS Lambda and utilize the assert module for, well, runtime assertions. We were seeing unexplainable timeouts whenever a falsy case was hit.

It was luck that we found this in our test suite because we ran it through Terser by mistake.

What is the expected behavior? Why is that the expected behavior?

I'd expect the column search to be much faster, closer to something in the sub second/milliseconds range.

What do you see instead?

It takes minutes to construct the assertion error message.

It looks like a subset of the code may be passed to acorn (https://github.com/nodejs/node/blob/main/lib/assert.js#L232) leading to syntax errors as it parses till the end (https://github.com/nodejs/node/blob/main/lib/assert.js#L265), acorn then reports that error whenever it hits that point (https://github.com/nodejs/node/blob/main/lib/assert.js#L278), findColumn then continues to the next token and reads more chunks if necessary and acorn starts from scratch.

A bundler like Webpack emits an IIFE which itself contains an immediate block. I believe that is the majority of time spent for acorn traversing each token in it. I suspect the many errors come from that very block that is incomplete, so acorn wastes time trying to parse something that is known to be incomplete.

I think a better regression to the reproduction section is to do an IIFE and drop semi colons in there: (() => {;;;;;;;...;;;;;;require('node:assert')(null);})(). Interestingly enough, an IIFE around it is not affected by it.

Another interesting thing is that this does not affect normal Error's - is there a particular reason why they run on two different implementations?

Additional information
Screenshot 2024-04-24 at 23 33 35

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。