Skip to content

Add util.diff to node:util - #8332

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-09-util-diff
Nov 12, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-09-util-diff

Conversation

@littledivy

@littledivy littledivy commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

node:util had no diff(), which Node added in v22.15.0 and v23.11.0 to expose the Myers diff behind assertion error messages.

In this patch:

  • util.diff(actual, expected) is ported from Node. It takes strings or arrays of strings and returns [op, value] entries in the same order as Node, with the same argument errors and size limit.
  • The Myers diff is ported from Node's lib/internal/assert/myers_diff.js, because the existing diff in internal_diffs.ts uses a different algorithm and produces a different order.
before after
util.diff(['1', '2', '3'], ['1', '3', '4']) TypeError (missing) [[0, '1'], [1, '2'], [0, '3'], [-1, '4']]
util.diff('foo', 'foo') TypeError (missing) []
util.diff(1, 'a') TypeError (missing) throws ERR_INVALID_ARG_TYPE

Tests are ported from Node's test-diff.js.

@littledivy
littledivy requested review from a team as code owners November 10, 2026 08:06

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 43d28681.

This review, of 3de4e16a

💬 Approved with comments — The port matches Node and is wired in correctly. One WARNING: memory grows quickly on large, very different inputs, as it does in Node. One INFO: the commit message does not record a compatibility decision for the new API.

Findings: 1 warning · 1 info (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8332.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Performance] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this.
- **File:** src/node/internal/internal_util_diff.ts:58

## INFO (1)

### [AGENTS.md] The new `util.diff` export adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance).
- **File:** src/node/util.ts:104

📖 AGENTS.md: 💬 1 info

The commit message does not record a compatibility decision for the new util.diff API.

Recommendation: 💬 Optional Nits

  • [INFO] The new util.diff export adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance). (src/node/util.ts:104)
View full review

The repository's guidelines ask that any addition to the API Workers can reach have its compatibility decision recorded in the commit message: either a compatibility flag, or an explicit statement that the risk is accepted. The commit message explains the port but records neither.


⚡ Performance: ⚠️ 1 warning

Each edit level saves a full copy of the V array, so memory grows quickly with large, very different inputs.

Recommendation: 📝 Minor Comments

  • [WARNING] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this. (src/node/internal/internal_util_diff.ts:58)
View full review

myersDiff saves a full Int32Array(2*(N+M)+1) copy into trace at every edit level. Memory is therefore about 8·D·(N+M) bytes, where D is the edit distance. Two completely different 2,000-line inputs need about 130 MB, which can exhaust a Worker's memory. The 2^31 - 1 check does not prevent this. Node behaves the same way.


✅ All other checks passed

🔒 Security: ✅ LGTM

validateString and validateStringArray run before the diff, so a wrong input type cannot reach the algorithm. The diff only uses memory in the calling Worker's own isolate; see the performance section.


📋 Code Quality: ✅ LGTM

The forward pass, the backtrack, validateInput and the exports match Node's implementation. The memory growth on large inputs is covered in the performance section.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 3de4e16a

Comment thread src/node/internal/internal_util_diff.ts Resolved
Comment thread src/node/util.ts Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-09-util-diff branch from 3de4e16 to 43d2868 Compare November 10, 2026 14:37

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of f9163537.

This review, of 43d28681

💬 Approved with comments — The port matches Node and is wired in correctly. One WARNING: memory grows quickly on large, very different inputs, as it does in Node. One INFO: the commit message does not record a compatibility decision for the new API.

Findings: 1 warning · 1 info

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8332.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Performance] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this.
- **File:** src/node/internal/internal_util_diff.ts:58

## INFO (1)

### [AGENTS.md] The new `util.diff` export adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance).
- **File:** src/node/util.ts:104

📖 AGENTS.md: 💬 1 info

The commit message does not record a compatibility decision for the new util.diff API.

Recommendation: 💬 Optional Nits

  • [INFO] The new util.diff export adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance). (src/node/util.ts:104)
View full review

The repository's guidelines ask that any addition to the API Workers can reach have its compatibility decision recorded in the commit message: either a compatibility flag, or an explicit statement that the risk is accepted. The commit message explains the port but records neither.


⚡ Performance: ⚠️ 1 warning

Each edit level saves a full copy of the V array, so memory grows quickly with large, very different inputs.

Recommendation: 📝 Minor Comments

  • [WARNING] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this. (src/node/internal/internal_util_diff.ts:58)
View full review

myersDiff saves a full Int32Array(2*(N+M)+1) copy into trace at every edit level. Memory is therefore about 8·D·(N+M) bytes, where D is the edit distance. Two completely different 2,000-line inputs need about 130 MB, which can exhaust a Worker's memory. The 2^31 - 1 check does not prevent this. Node behaves the same way.


✅ All other checks passed

🔒 Security: ✅ LGTM

validateString and validateStringArray run before the diff, so a wrong input type cannot reach the algorithm. The diff only uses memory in the calling Worker's own isolate; see the performance section.


📋 Code Quality: ✅ LGTM

The forward pass, the backtrack, validateInput and the exports match Node's implementation. The memory growth on large inputs is covered in the performance section.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 43d28681 — same changes as 3de4e16a, so this is that commit's review (no new review was run; line numbers are as of 3de4e16a)

Comment thread src/node/internal/internal_util_diff.ts Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 0c9e022b.

This review, of f9163537

✅ Approved — The port matches Node and is wired in correctly. Both notes from the last review were settled in the discussion, and no new issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Input is validated before it reaches the diff algorithm.


📖 AGENTS.md: ✅ LGTM

@​jasnell [approver] said no compatibility flag is needed for the new diff export, so that decision is recorded on this pull request.


📋 Code Quality: ✅ LGTM

The forward pass, the backtrack, validateInput and the exports match Node's implementation.


⚡ Performance: ✅ LGTM

myersDiff keeps a copy of the V array for every edit level, as Node's version does. Memory use is bounded by the isolate heap cap and stays inside the calling Worker's own isolate.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: f9163537

littledivy and others added 2 commits November 11, 2026 22:46
node:util had no diff(), which Node.js added in v22.15.0 and v23.11.0
to expose the Myers diff behind assertion error messages.

In this patch util.diff(actual, expected) is ported from Node.js. It
accepts strings or arrays of strings and returns [op, value] entries
in the same order as Node.js.

- Port the Myers diff from lib/internal/assert/myers_diff.js, since
  the existing diff in internal_diffs.ts uses a different algorithm
  and does not produce the same ordering.
- Port test/parallel/test-diff.js into util-nodejs-test.js.

util.diff is a new export of node:util, which is only available under
nodejs_compat, so it ships without a compatibility flag: code that
doesn't call it can't observe it, as with earlier node:util additions.
Like Node.js, the diff keeps one vector per edit step, so memory grows
with the product of input size and edit distance, and the input size
limit is Node's.
Co-authored-by: James M Snell <jsnell@cloudflare.com>
@littledivy
littledivy force-pushed the littledivy/2026-11-09-util-diff branch from f916353 to 0c9e022 Compare November 11, 2026 17:31

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — The util.diff port matches Node's Myers diff and argument checks, and the export and tests are wired in correctly. No new issues since the last review.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Input is validated before it reaches the diff algorithm.


📖 AGENTS.md: ✅ LGTM

@​jasnell [approver] said the new diff export on node:util does not need a compatibility flag, so that decision is recorded on this pull request.


📋 Code Quality: ✅ LGTM

The forward pass, the backtrack, validateInput and the util.diff export match Node's implementation. The ported tests cover the documented ordering, the identical-input case and the argument errors.


⚡ Performance: ✅ LGTM

myersDiff keeps a copy of the V array for every edit level, as Node's version does. As @​jasnell [approver] noted in the discussion, the isolate heap cap bounds this memory, and it stays inside the calling Worker's own isolate. The changes since the last review touch only the util.ts export and the test file.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 0c9e022b

@littledivy
littledivy merged commit 5dc4754 into main Nov 12, 2026
23 of 24 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-09-util-diff branch November 12, 2026 01:55
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