How big a file can a browser tab actually process?
A converter that works on a 50 KB image and fails on a 200 KB one is a familiar bug report. The trace usually points at String.fromCharCode.apply or btoa, and the first instinct is that the file is “too large”. That is almost never the real limit.
I measured where it actually breaks in Chromium 156, on one ordinary machine, on 19 November 2026. These numbers are from that run — not a benchmark of your device, and not a claim about other browsers.
The symptom
A page reads a file, converts the bytes to base64, and posts the result to an API. Small files work. Somewhere around a couple of hundred kilobytes it throws:
RangeError: Maximum call stack size exceeded
Nothing in that message mentions file size. The failure is in how the bytes are passed to String.fromCharCode.
The mechanism
JavaScript engines limit how many arguments a function call can take. String.fromCharCode.apply(null, bytes) spreads every byte of the array into the argument list. Past a certain count the call throws before any conversion happens.
The usual recipe is:
const base64 = btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
One call, every byte at once. Fine for a small avatar, fatal for a scanned form.
What the measurements show
| Input | Method | Result | Time |
|---|---|---|---|
| 1,048,576 characters |
btoa alone |
ok | 23.1 ms |
| 65,536 bytes | String.fromCharCode.apply |
ok | — |
| 131,071 bytes | String.fromCharCode.apply |
RangeError | — |
| 131,072 bytes | String.fromCharCode.apply |
RangeError | — |
| 5 MB bytes → 32 KB chunks | chunked loop + btoa
|
ok, 6,990,508-char string | 250.9 ms |
Two things fall out of it.
First, btoa itself is not the problem — it converted a one-megabyte string in tens of milliseconds. The limit is the argument spread, and it lands between 65,536 and 131,071 bytes. The practical ceiling is 65,536, because the next power of two already fails.
Second, file size is the wrong variable. A 5 MB file converts fine when the bytes are handed over in 32 KB pieces; the same code dies on a 128 KB file when they are handed over at once.
The working pattern
Chunk the bytes, convert each chunk, join:
function bytesToBase64(bytes) {
const CHUNK = 0x8000; // 32,768
let binary = '';
for (let i = 0; i < bytes.length; i += CHUNK) {
binary += String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK));
}
return btoa(binary);
}
0x8000 sits comfortably under the observed limit, and 32 KB chunks keep string concatenation cheap enough for multi-megabyte inputs.
Two consequences worth planning for:
- The output is larger than the input. Base64 encodes three bytes as four characters, so 5 MB becomes roughly 6.99 million characters — the number above, not an estimate. Both the binary string and the base64 string exist at once.
-
The string, not the file, is what you hold. A 50 MB upload means about 67 million characters in memory before anything is sent. Streaming or
FileReaderchunking avoids that; onebtoaat the end does not.
What this test does not prove
- One engine only. Chromium 156 was measured. Firefox and Safari have their own argument limits and string handling; both need real runs before anyone quotes a cross-browser threshold.
- Timing is not a benchmark. 23.1 ms and 250.9 ms describe one machine under no load. Treat them as orders of magnitude.
- Memory ceilings were not tested. When a tab runs out of memory depends on the device, the tab's other work, and GC behaviour.
-
crypto.subtle.digeston a 20 MB array threw aTypeErrorhere and the cause was not isolated. It is mentioned only to say it is unresolved, not to imply a limit.
A short checklist
- Chunk at 32 KB (64 KB at most) before
String.fromCharCode. - Never spread a megabyte-scale typed array into a function call.
- Remember base64's 4/3 expansion when sizing buffers.
- Test on the slowest device you support.
- If a converter fails near 128 KB, suspect the argument spread before the file.
The measurement that matters is the one on your target device. The pattern above removes the artificial limit; the real one — memory — is your device's to decide.
Drafted with AI assistance from tests run on 19 November 2026; the numbers and code were checked before publishing.
Top comments (0)