Skip to content

Worse-than-linear assert performance for trivial mismatch #62792

Open

Description

@joepie91

Version

v24.14.0

Platform

Linux desktop-home 6.12.77 #1-NixOS SMP PREEMPT_DYNAMIC Fri Mar 13 16:20:49 UTC 2026 x86_64 GNU/Linux

Subsystem

assert

What steps will reproduce the bug?

"use strict";

const assert = require("node:assert");

(async () => {
	let perf1000bytes;
	
	for (let i = 1000; i < 35000; i += 1000) {
		let buffer = Buffer.alloc(i);
		// console.log(i);
		
		let start = Date.now();
		try {
			assert.strictEqual(buffer, [buffer]);
		} catch (_error) {}
		let time = Date.now() - start;

		if (i === 1000) {
			perf1000bytes = time;
			console.log(`${i} bytes: ${time}ms`);
		} else {
			let expected = (i / 1000) * perf1000bytes;
			let worse = (((time / expected) * 100) - expected).toFixed(2);

			console.log(`${i} bytes: ${time}ms (${worse}% worse than linear)`);
		}
	}
})();

Output shows Buffer length, how long it took to make (and fail) the comparison, and how much worse-than-linear it is compared to the 1000-byte case.

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

Reliably on my system, when comparing a Buffer and an array containing a Buffer (and nothing else). Encountered it organically while writing a test for a project. Not tested with other data structures. Occurs with both strictEqual and deepStrictEqual (especially the former is unexpected).

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

Should ideally immediately recognize that a Buffer and an array are not the same thing and fail the assertion, or fail upon realizing that the two are not the same length at all, or at worst be linear in its performance (one of the inputs only has a single item after all).

What do you see instead?

1000 bytes: 51ms
2000 bytes: 187ms (81.33% worse than linear)
3000 bytes: 381ms (96.02% worse than linear)
4000 bytes: 662ms (120.51% worse than linear)
5000 bytes: 962ms (122.25% worse than linear)
6000 bytes: 1395ms (149.88% worse than linear)
7000 bytes: 1773ms (139.64% worse than linear)
8000 bytes: 2308ms (157.69% worse than linear)
9000 bytes: 2925ms (178.25% worse than linear)
10000 bytes: 3634ms (202.55% worse than linear)
11000 bytes: 4294ms (204.42% worse than linear)
12000 bytes: 5153ms (229.99% worse than linear)
13000 bytes: 5961ms (236.10% worse than linear)
14000 bytes: 7058ms (274.52% worse than linear)
15000 bytes: 7878ms (264.80% worse than linear)
16000 bytes: 9034ms (291.11% worse than linear)
17000 bytes: 10457ms (339.11% worse than linear)
18000 bytes: 12672ms (462.39% worse than linear)
19000 bytes: 14424ms (519.54% worse than linear)
20000 bytes: 16561ms (603.63% worse than linear)
21000 bytes: 18757ms (680.35% worse than linear)
22000 bytes: 20738ms (726.31% worse than linear)

Additional information

No response

Activity

  1. added
    assertIssues and PRs related to the assert subsystem.
    on Apr 18, 2026
  2. added
    performanceIssues and PRs related to the performance of Node.js.
    on Apr 20, 2026
  3. BridgeAR commented on Apr 25, 2026

    @BridgeAR
    Member

    The comparison is actually fast, it is the assertion error that is slow. It uses a non ideal myers implementation.

    I agree that we should make this faster overall. I am currently looking into improving the performance.

  4. github-actions commented on Jul 25, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  5. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 25, 2026
  6. Kamleshbarik05 commented on Aug 1, 2026

    @Kamleshbarik05

    Hi @BridgeAR , I see this issue was marked as stale, but the performance bottleneck is still very relevant. I would love to help fix this and look into optimizing the Myers diff implementation in the assertion error formatter.

    Could you please assign this to me, or point me in the direction of the files I should look at? Thanks!

  7. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 2, 2026
  8. self-assigned this
    on Aug 25, 2026
  9. maciejkarpinskiux commented on Oct 16, 2026

    @maciejkarpinskiux

    Another data point, because the failure mode can be worse than slow: it can take the whole machine down.

    • Node v26.4.0, macOS 26.5.1, Apple M1 Pro, 16 GB.
    • Two Uint8ClampedArrays of 259,200 elements (a 360 x 180 RGBA image), ~21k elements differing, compared with assert.deepEqual (strict mode) inside node --test.
    • The comparison failed as intended (a mutation test), and building the AssertionError diff grew the process to 28.7 GB resident. The memory compressor hit its limit and the kernel panicked ("watchdog timeout: no checkins from watchdogd in 92 seconds"). The panic report names that one node process as by far the largest.

    Smaller sizes show the curve (same machine, mismatch on every third element):

    elements time to build the error RSS
    2,000 34 ms 102 MB
    8,000 458 ms 568 MB
    16,000 1,781 ms 680 MB
    import assert from "node:assert/strict";
    const n = Number(process.argv[2]);
    const a = new Uint8ClampedArray(n), b = new Uint8ClampedArray(n);
    for (let i = 0; i < n; i++) { a[i] = i % 7; b[i] = i % 3 === 0 ? 9 : a[i]; }
    try { assert.deepEqual(a, b); } catch {}

    A cap on the diff (fall back to "first difference at index i" past some length) would turn this from a machine crash into a readable failure. Workaround on our side: compare large arrays element by element and report the first mismatch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

assertIssues and PRs related to the assert subsystem.performanceIssues and PRs related to the performance of Node.js.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions