Skip to content

assert.ok() becomes slow when using the Hooks API #52962

Open
@privatenumber

Description

@privatenumber

Version

v20.13.1

Platform

No response

Subsystem

Mac/Linux

What steps will reproduce the bug?

node --import ./hook.mjs file.mjs

hook.mjs:

import { isMainThread } from 'worker_threads';
import module from 'module';

if (isMainThread) {
	module.register(import.meta.url, import.meta.url);
}

export const load = async (
	url,
	context,
	nextLoad,
) => {
	if (url.endsWith('/file.mjs')) {
		return {
			shortCircuit: true,
			format: 'module',
			// Originally, this is from esbuild compiling the input. But inlining the compiled code here demonstrates that's not the issue.
			source: 'import assert from"assert";const startTime=Date.now();console.log("start time:",Date.now()-startTime);try{assert.ok(false)}finally{console.log("end time:",Date.now()-startTime)}',
		};
	}
	return await nextLoad(url, context);
};

file.mjs: https://github.com/privatenumber/node-bug-slow/blob/develop/file.mjs
(The minimal reproduction requires a file with a massive comment so I decided not to paste here)

I uploaded the minimal reproduction here as well:
https://github.com/privatenumber/node-bug-slow

And it takes 1.3s to run on GitHub Actions:
https://github.com/privatenumber/node-bug-slow/actions/runs/9057737597/job/24882195019

How often does it reproduce? Is there a required condition?

Consistently.

But the reproduction is fragile. Deleting a space here and there would make it run expectedly under 10ms.

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

It should take under 10ms.

What do you see instead?

The assert.ok(false) call takes around 600ms for me, and 1.5s on GitHub Actions.

Additional information

Originally reported in privatenumber/tsx#548

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.confirmed-bugIssues with confirmed bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions