Skip to content

Conversation

@JeremyMoeglich
Copy link

Previously getNthKey might be called many times each time iterating through a full user provided collection.
By providing a large collection and a large referentialEqualities array you could cause quadratic compute.
For a 1mb payload (a common webserver limit) this can block the event loop for 20 - 40 seconds

Benchmarks (run on a Ryzen 7 5700X, Arch Linux)
Previous

NodeJS:
baseline: 37.97 ms
small attack: 439.44 ms
double inputs: 1695.84 ms (3.85x)
optimized: 30912.67 ms

Bun:
baseline: 28.13 ms
small attack: 524.92 ms
double inputs: 2063.38 ms (3.93x)
optimized: 45527.36 ms

After this change:

NodeJS:
baseline: 62.29 ms
small attack: 14.02 ms
double inputs: 17.99 ms (1.28x)
optimized: 101.53 ms

Bun:
baseline: 52.97 ms
small attack: 17.19 ms
double inputs: 27.44 ms (1.60x)
optimized: 94.58 ms

@Skn0tt
Copy link
Collaborator

Skn0tt commented Dec 17, 2025

Hi Jeremy, thanks for your contribution. I don‘t like how this minor fix significantly increases complexity. Let‘s discuss the problem in an issue and see what are possible alternatives.

@JeremyMoeglich JeremyMoeglich marked this pull request as draft December 17, 2025 21:16
@JeremyMoeglich JeremyMoeglich marked this pull request as ready for review December 18, 2025 05:23
@JeremyMoeglich
Copy link
Author

This PR also seems to have a regression I'll have to fix if we want to take this approach over yours.
Though I would still prefer to take your approach as that reduces code quite a bit.

import superjson from 'superjson';

const set = new Set();
const root = { back: set };

set.add(null);
set.add(root);

try {
  const json = superjson.stringify(root);
  const _ = superjson.parse(json);
  console.log("✅ Success");
} catch (e) {
  console.error("❌ Crash detected:", e.message);
}

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants