What is a Blob?

A Blob (Binary Large Object) is an immutable, raw‑data container supplied by the browser. It can hold binary or textual data and exposes methods for streaming, slicing, and type inspection.

💡 Notes: Because Blobs are immutable, every operation that modifies content (slice(), for instance) returns a new Blob.

Constructor: new Blob()

Syntax

const blob = new Blob(blobParts?, options?);

Parameters

Example

// create a PNG payload
const pngBlob = new Blob([Uint8ArrayView], {type: "image/png"});

Blob Properties

PropertyTypeDescription
size number Length in bytes; 0 for empty Blobs.
type string MIME type. Empty string if unknown.

Instance Methods

arrayBuffer()

text()

stream()

slice(start?, end?, type?)

// first 1 MB of video:
const preview = videoBlob.slice(0, 1_048_576, "video/mp4");

URL Helpers

URL.createObjectURL(blob)

Creates a short‑lived blob: URL pointing to in‑memory data.

URL.revokeObjectURL(url)

Manually release memory when the Blob URL is no longer needed.


// Display dynamically‑generated image
const url = URL.createObjectURL(pngBlob);
img.src = url;

// Later…
URL.revokeObjectURL(url);

File Extension & FileReader

File ≅ Blob + Name/Metadata

const file = new File(blobParts, "report.pdf", {type: "application/pdf", lastModified: Date.now()});

FileReader Methods

⚠️ Notes: FileReader is main‑thread‑only; prefer blob.arrayBuffer() in modern code, especially inside Web Workers.

Fetch API Integration

// POST raw audio to server
fetch("/upload", {
	method: "POST",
	headers: {"Content-Type": "audio/wav"},
	body: audioBlob            // Blob streams automatically
});

Conversely, responses expose response.blob(), response.arrayBuffer(), or response.text() for decoding.

Performance & Best Practices

Browser Support (May 2025)

All evergreen browsers fully support core Blob APIs. Streaming (stream()) requires:

Quick Reference Cheat‑Sheet

// create
const b = new Blob([data], {type:"application/json"});

// inspect
b.size;           // bytes
b.type;           // "application/json"

// consume
await b.text();   // string
await b.arrayBuffer();
const stream = b.stream();

// mutate (returns new Blob)
const head = b.slice(0, 512);

// object‑URL
const url = URL.createObjectURL(b);
URL.revokeObjectURL(url);