Data serialization · · updated
Binary file slicing: how the Blob API reads large files locally
A File is a Blob with a name and lastModified. slice(start, end, type) yields another Blob that reads that byte range from disk (or memory) when you request arrayBuffer. That is how a web page hashes, zips, or peeks at a multi-hundred-megabyte file without a server filesystem. It is not mmap, and mobile Safari will still say no at some size. See create ZIP, file hash calculator, and merge PDF.
Blob, File, and slice
File extends Blob. slice is inclusive-start exclusive-end. type can be forced for a sliced ZIP local file header peek. This is the substrate under chunked checksums and under sequential PDF merge: load one ArrayBuffer, copy pages, drop it, load the next.
Consumers: zip and PDF
JSZip can load a Blob. Create ZIP and extract ZIP should not POST the archive. pdf-lib wants ArrayBuffer of each PDF. Do not File.slice a PDF to “split pages”; that corrupts xref. Split is a page-tree copy in split PDF.
Mobile cliffs
iOS WebKit memory. Decode of a 50 MP image is worse than slicing a zip. Downsample at decode time (createImageBitmap resizeWidth). CSV grids that materialize every cell will die before Blob.slice does — keep huge tables as text windows.
createObjectURL versus Base64
Short-lived blob: URLs for <a download> and <img>. Revoke in finally. Do not put blob URLs in analytics. Data URLs are the anti-pattern in Base64 vs Blobs.
Architecture
There is no reason to POST a 2 GB file to convert it if the codec path exists in-tab. Upload-convert-download is a business model, not a technical requirement. Minimization is the legal name for that fact. Network-tab is the test.
Tools
File hash calculator, create ZIP, extract ZIP, merge PDF, image compressor.
Worked example: merge three large PDFs without OOM
Do not load three 80 MB ArrayBuffers at once. Load A, create an empty PDFDocument, copyPages, drop A’s buffer (let it GC), load B, copy, drop, load C. Merge PDF should be sequential for a reason. That is Blob-backed loading, not page-range File.slice. Page split remains a catalog edit in split PDF and the PDF merge article.
Zip of images: create ZIP from File objects the user selected. JSZip will read them; you still should not Base64 each entry (Base64 pitfalls). Extract: extract ZIP, download individual Blobs, revoke URLs.
Images: slice does not reduce megapixels. You must downsample in canvas / compressor. Hashing the original with file hash calculator is cheap compared to decode.
When a tab dies, it is almost always decode or JSON.parse, not slice itself. Catch QuotaExceededError, show a local error, do not “fall back” to upload. Minimization includes not exfiltrating the file because the tab was tired.
Frequently asked questions
Does slice copy the bytes immediately?
It creates a new Blob that refers to a range. The copy happens when you read it. Holding many slices still costs bookkeeping.
What is the size limit?
Engine and device dependent. Desktop browsers handle multi-GB better than iOS. Always catch QuotaExceededError. Images still hit canvas caps — Canvas processing.
Why do object URLs leak?
URL.createObjectURL pins the Blob. URL.revokeObjectURL after the download or preview.