From 254ebd924c321048f16f2dada43777dccb63696e Mon Sep 17 00:00:00 2001 From: Yaroslav Chuhai Date: Thu, 19 Nov 2026 20:45:15 +0100 Subject: [PATCH] esm: skip percent-decode copy for unescaped data URLs 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 --- benchmark/esm/data-url-processor.js | 52 +++++++++++++++++++++++++++++ lib/internal/data_url.js | 35 ++++++++++++++----- 2 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 benchmark/esm/data-url-processor.js diff --git a/benchmark/esm/data-url-processor.js b/benchmark/esm/data-url-processor.js new file mode 100644 index 000000000000..227ef49df671 --- /dev/null +++ b/benchmark/esm/data-url-processor.js @@ -0,0 +1,52 @@ +// Benchmarks dataURLProcessor(), which decodes the body of `data:` URLs used +// by import() and Worker. `plain` and `base64` bodies contain no '%' and so +// take the unescaped path; `percent` bodies are percent-decoded, and +// `percent-tail` bodies have a single escape at the very end. +'use strict'; + +const assert = require('node:assert'); +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + n: [1e4], + size: [64, 1024, 16384, 65536], + encoding: ['plain', 'percent', 'percent-tail', 'percent-mid', 'percent-at-64', 'base64'], +}, { flags: ['--expose-internals'] }); + +function main({ n, size, encoding }) { + const { dataURLProcessor } = require('internal/data_url'); + let source = `export default '${'a'.repeat(size)}';`; + let body = source; + let mime = 'text/javascript'; + if (encoding === 'percent') { + body = encodeURIComponent(source); + } else if (encoding === 'percent-tail') { + body = `${source}%0A`; + source += '\n'; + } else if (encoding === 'percent-mid') { + const half = source.length >> 1; + body = `${source.slice(0, half)}%0A${source.slice(half)}`; + source = `${source.slice(0, half)}\n${source.slice(half)}`; + } else if (encoding === 'percent-at-64') { + body = `${'x'.repeat(64)}${encodeURIComponent(source)}`; + source = `${'x'.repeat(64)}${source}`; + } else if (encoding === 'base64') { + body = Buffer.from(source).toString('base64'); + mime += ';base64'; + } + const url = new URL(`data:${mime},${body}`); + let result; + + for (let i = 0; i < 1000; i++) { + dataURLProcessor(url); + } + + bench.start(); + for (let i = 0; i < n; i++) { + result = dataURLProcessor(url); + } + bench.end(n); + + assert.strictEqual(result.mimeType.essence, 'text/javascript'); + assert.deepStrictEqual(result.body, new TextEncoder().encode(source)); +} diff --git a/lib/internal/data_url.js b/lib/internal/data_url.js index 0aea5030ecad..a05d22dcce87 100644 --- a/lib/internal/data_url.js +++ b/lib/internal/data_url.js @@ -7,6 +7,7 @@ const { StringPrototypeCharCodeAt, StringPrototypeIndexOf, StringPrototypeSlice, + TypedArrayPrototypeSet, TypedArrayPrototypeSubarray, Uint8Array, } = primordials; @@ -178,8 +179,13 @@ function stringPercentDecode(input) { // 1. Let bytes be the UTF-8 encoding of input. const bytes = lazyEncoder().encode(input); + const firstPercent = StringPrototypeIndexOf(input, '%'); + if (firstPercent === -1) { + return bytes; + } + // 2. Return the percent-decoding of bytes. - return percentDecode(bytes); + return percentDecode(bytes, bytes.length === input.length ? firstPercent : 0); } /** @@ -209,21 +215,31 @@ function hexByteToNumber(byte) { // https://url.spec.whatwg.org/#percent-decode /** * @param {Uint8Array} input + * @param {number} [start] Index of the first 0x25 (%) byte, if known. * @returns {Uint8Array} */ -function percentDecode(input) { +function percentDecode(input, start = 0) { const length = input.length; // 1. Let output be an empty byte sequence. /** @type {Uint8Array} */ const output = new Uint8Array(length); + let src = input; + let dst = output; + const offset = start > 128 ? start : 0; + if (offset > 0) { + TypedArrayPrototypeSet(output, TypedArrayPrototypeSubarray(input, 0, offset)); + src = TypedArrayPrototypeSubarray(input, offset); + dst = TypedArrayPrototypeSubarray(output, offset); + } + const srcLen = src.length; let j = 0; // 2. For each byte byte in input: - for (let i = 0; i < length; ++i) { - const byte = input[i]; + for (let i = 0; i < srcLen; ++i) { + const byte = src[i]; // 1. If byte is not 0x25 (%), then append byte to output. if (byte !== 0x25) { - output[j++] = byte; + dst[j++] = byte; // 2. Otherwise, if byte is 0x25 (%) and the next two bytes // after byte in input are not in the ranges @@ -232,16 +248,16 @@ function percentDecode(input) { // to output. } else if ( byte === 0x25 && - !(isHexCharByte(input[i + 1]) && isHexCharByte(input[i + 2])) + !(isHexCharByte(src[i + 1]) && isHexCharByte(src[i + 2])) ) { - output[j++] = 0x25; + dst[j++] = 0x25; // 3. Otherwise: } else { // 1. Let bytePoint be the two bytes after byte in input, // decoded, and then interpreted as hexadecimal number. // 2. Append a byte whose value is bytePoint to output. - output[j++] = (hexByteToNumber(input[i + 1]) << 4) | hexByteToNumber(input[i + 2]); + dst[j++] = (hexByteToNumber(src[i + 1]) << 4) | hexByteToNumber(src[i + 2]); // 3. Skip the next two bytes in input. i += 2; @@ -249,7 +265,8 @@ function percentDecode(input) { } // 3. Return output. - return length === j ? output : TypedArrayPrototypeSubarray(output, 0, j); + const written = offset + j; + return length === written ? output : TypedArrayPrototypeSubarray(output, 0, written); } // https://infra.spec.whatwg.org/#forgiving-base64-decode