Browser Engineering

Incremental SHA-256 in a Web Worker Without Freezing the UI

Design chunked reads, transferable buffers, incremental digests, throttled progress, cancellation, and Worker reuse for large files and mobile memory pressure.

A capability stays maintainable only when the team can explain every state, retry, and piece of residual data—not merely show one successful run. Reading a whole file makes memory scale with file size, while main-thread hashing creates long tasks. A bounded read window and incremental Worker state avoid both.

Browser capability depends on version, permission, visibility, and device policy. Use capability detection and an explained fallback instead of treating one development machine as the product contract.

Questions the design must answer

List non-negotiable invariants before selecting performance knobs. Tuning can roll out gradually; identity, permission, and terminal-state rules cannot drift at runtime.

  • Read bounded chunks and transfer ArrayBuffer ownership, releasing budget only after processing.
  • Throttle progress by time rather than posting every chunk back to the main thread.
  • Version cancel messages so the Worker drops hash state and rejects stale read callbacks.

A Worker addresses thread contention; windowing, ownership, and cancellation address resource safety. All are required.

Edge cases are part of the feature

Prioritize faults that silently preserve false facts: the interface looks recovered while a queue, permission, or counter has diverged. The defect often appears only on the next action.

  • Transfer and hash independently reread the file, doubling I/O and memory traffic.
  • The main thread retains a view after transfer and reads a detached buffer during retry.
  • One global Worker mixes two tasks, corrupting digest state and progress.

Prove that it works with evidence

Do not stop verification when the final action succeeds. Count side effects, measure wait time, inspect privacy, and prove the next run begins from a clean baseline.

  1. Hash a file larger than available memory and prove peak use follows the window.
  2. Cancel at every chunk boundary and start a new task; old results must never reach it.
  3. Measure input delay and long tasks while hashing; interaction and screen viewing must remain responsive.

The release bar is clear: users understand the current state, failures stop or recover, resources stay bounded, and operators can identify the phase from minimum necessary evidence.

Put the guide to work

Open uCopy and connect two devices securely from the browser.

Start for free