Optimize MHTML parser: 1.5x faster on large files#1928
Draft
sweenzor wants to merge 1 commit intogildas-lormeau:masterfrom
Draft
Optimize MHTML parser: 1.5x faster on large files#1928sweenzor wants to merge 1 commit intogildas-lormeau:masterfrom
sweenzor wants to merge 1 commit intogildas-lormeau:masterfrom
Conversation
Three targeted performance fixes in the JS MHTML parser hot path:
1. parse.js: Replace splice(...spread) with push loop for byte
accumulation. The old `resource.data.splice(len, 0, ...next)`
spread a Uint8Array into individual arguments on every line of
MHTML content — O(n) per call in a tight loop over thousands of
lines. A simple `push(next[i])` loop avoids the spread overhead
entirely. Truncation via `data.length -= N` replaces splice for
quoted-printable soft line break removal.
2. util.js decodeBinary: Replace character-by-character string
concatenation (`data += String.fromCharCode(byte)`) with chunked
`String.fromCharCode.apply(null, chunk)` joined at the end. The
old approach was O(n²) due to string immutability; each `+=`
allocated a new string. Chunks of 8192 bytes stay within the
call stack limit for `apply`.
3. util.js decodeBase64: Replace `atob(v).split("").map(c =>
c.charCodeAt(0))` with a pre-allocated Uint8Array filled via a
direct for-loop. The old approach created two intermediate arrays
(one from split, one from map) that were immediately discarded.
Benchmarked on synthetic MHTML fixtures (before → after):
- 10KB: 0.51ms → 0.37ms (1.4x)
- 1MB: 63.2ms → 43.1ms (1.5x)
- 10MB: 572ms → 423ms (1.4x)
- 100MB: 6063ms → 3982ms (1.5x)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Speeds up MHTML parsing ~1.5x on large files via three changes:
splice(len, 0, ...next)with apush()loop; uselengthtruncation for soft line breaks.decodeBinary— Build string in 8KB chunks viaString.fromCharCode.apply()instead of char-by-char.decodeBase64— DirectUint8Arrayloop instead ofatob().split("").map(). Added try/catch for malformed input.To reproduce, save a page as MHTML from Chrome, then: