Skip to content

esm: skip percent-decode copy for unescaped data URLs - #67684

Open
ychuhai wants to merge 1 commit into
nodejs:mainfrom
ychuhai:pref/esm-data-url-fast-path
Open

ychuhai wants to merge 1 commit into
nodejs:mainfrom
ychuhai:pref/esm-data-url-fast-path

Conversation

@ychuhai

@ychuhai ychuhai commented Nov 21, 2026

Copy link
Copy Markdown

Decoding a data: URL body copied every byte into a second array, even when there was nothing to decode. Skip when there's nothing, and take the index when there is a percent and use that to start the encoding.

benchmark/esm/data-url-processor.js (30 runs):

size plain base64
64 +30.23 % *** +18.97 % ***
1024 +146.98 % *** +27.66 % ***
16384 +1029.18 % *** +24.37 % ***
65536 +1125.72 % *** +27.25 % ***
size percent percent-tail percent-mid percent-at-64
64 −2.02 % −1.13 % +4.29 % +3.84 %
1024 +6.32 % +113.54 % *** +36.46 % *** +6.23 % *
16384 +11.23 % *** +804.23 % *** +83.44 % *** +11.17 % ***
65536 +15.25 % *** +968.96 % *** +87.65 % *** +7.13 %

stringPercentDecode() UTF-8 encodes its input into a fresh Uint8Array
and then percent-decodes it into a second array of the same size. After
URL parsing, a data: URL body contains '%' only when it has non-ASCII or
control characters or explicit escapes, so plain-text and base64 bodies
were copied byte-for-byte for nothing.

Return the encoded bytes directly when the input contains no '%'. UTF-8
never emits 0x25 inside a multi-byte sequence, so the result is
identical, and each call still returns its own buffer.

When there is a '%', pass its index to percentDecode(), which copies a
long unescaped prefix in bulk and decodes the rest through subarray
views, so the loop still starts at 0 and keeps its bounds checks
eliminated. The string index is used only when every code point encoded
to a single byte, so it is also the byte index.

Signed-off-by: Yaroslav Chuhai <yaroslavchuhai@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Nov 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

@gurgunday gurgunday added performance Issues and PRs related to the performance of Node.js. needs-benchmark-ci PRs that need a benchmark CI run. labels Nov 21, 2026
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.60%. Comparing base (db671bb) to head (254ebd9).
⚠️ Report is 101 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67684      +/-   ##
==========================================
- Coverage   90.71%   90.60%   -0.12%     
==========================================
  Files         801      801              
  Lines      280917   281107     +190     
  Branches    54237    54269      +32     
==========================================
- Hits       254832   254691     -141     
- Misses      16253    16601     +348     
+ Partials     9832     9815      -17     
Files with missing lines Coverage Δ
lib/internal/data_url.js 100.00% <100.00%> (ø)

... and 69 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

nice!

@gurgunday gurgunday added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. needs-benchmark-ci PRs that need a benchmark CI run. needs-ci PRs that need a full CI run. performance Issues and PRs related to the performance of Node.js. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants