# TypedArrays, ArrayBuffer, and Zero-Copy Transfers vs Web Crypto API and SubtleCrypto

- **Canonical URL:** https://frontend.rocks/vs/typed-arrays-zero-copy-vs-web-crypto-subtle/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** TypedArrays transfer raw buffers to workers for custom math, whereas crypto.subtle processes ArrayBuffers natively off-thread.

## Comparison Matrix

| Dimension | TypedArrays, ArrayBuffer, and Zero-Copy Transfers | Web Crypto API and SubtleCrypto |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | JS Runtime | Web APIs |
| **Mental Model** | Contiguous binary buffers (ArrayBuffer) paired with unboxed numeric views (Uint8Array, Float32Array) that transfer across workers in O(1) via ownership handoff. | Native C++-backed cryptographic primitives (AES-GCM, SHA-256, ECDSA, HKDF) running off the JS thread in secure contexts with non-extractable CryptoKey handles. |
| **Prerequisites** | Structured Clone Algorithm (Transferable ArrayBuffers use the Structured Clone transfer list to move ownership without copying.); JavaScript Generational Garbage Collection (Pre-allocated TypedArray views eliminate per-element heap boxing and Young Generation GC churn.) | Event Loop, Macrotasks, and Microtask Queue (SubtleCrypto returns Promises that resolve on the microtask queue after native thread work.) |
| **When Not to Use** | When not to use TypedArray: Avoid TypedArray for small, sparse, or mixed-type lists, creating a backing ArrayBuffer costs more than a small literal [1, 2, 3]; for hashing or encryption over binary buffers, pass the ArrayBuffer directly to Web Crypto (SubtleCrypto). | When not to use SubtleCrypto: Never roll custom client-side password hashing to replace server-side Argon2id or TLS transport security. |

## TypedArrays, ArrayBuffer, and Zero-Copy Transfers — Under the Hood
Picture handing over the keys to a locked storage unit instead of unpacking every box into a second truck: the recipient gets instant access, and your copy of the key snaps in half (`detached`).

1. **Buffer vs View**: `ArrayBuffer` holds raw bytes; `Uint8Array` or `Float32Array` slices a typed window (`byteOffset`, `byteLength`) over it.
2. **Zero Boxing**: Unlike `Array`, elements never allocate heap wrappers or stress [Garbage Collection](/concepts/js-garbage-collection).
3. **Transferable Move**: Listing `buffer` in `postMessage(msg, [buffer])` detaches the sender's view (`byteLength === 0`) and hands the pointer to the worker.

```js
const raw = new ArrayBuffer(16);
const view = new DataView(raw);
view.setUint32(0, 0xdeadbeef, false); // Explicit big-endian

// Zero-copy handoff to worker
worker.postMessage(raw, [raw]);
console.log(raw.byteLength); // 0 (detached)
```

## Web Crypto API and SubtleCrypto — Under the Hood
`SubtleCrypto` is a bank safe-deposit box with a slot in the door: you slide plaintext in, and ciphertext slides out, while the key inside can be marked `extractable: false` so even an [XSS](/concepts/dom-xss-trusted-types) script cannot read the raw key bytes.

1. **Native Offload**: Calls dispatch to an internal browser crypto thread pool, resolving Promises on the [Event Loop](/concepts/event-loop-tasks-microtasks) without blocking frames.
2. **Non-Extractable Keys**: Keys generated or imported with `extractable: false` can be stored in [IndexedDB](/concepts/indexeddb-transactions-quotas) and used to sign or decrypt, yet `exportKey()` rejects.
3. **Hardware Acceleration**: Engines route AES-GCM and SHA-256 to CPU AES-NI / ARMv8 crypto instructions.

```js
const key = await crypto.subtle.generateKey(
  { name: 'AES-GCM', length: 256 },
  false, // non-extractable: JS can never read raw key bytes
  ['encrypt', 'decrypt']
);
const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt(
  { name: 'AES-GCM', iv },
  key,
  new TextEncoder().encode('sensitive-payload')
);
```

## Interactive Labs & Dissections
- [TypedArrays, ArrayBuffer, and Zero-Copy Transfers Lab](https://frontend.rocks/concepts/typed-arrays-zero-copy/#lab) ([Markdown](https://frontend.rocks/concepts/typed-arrays-zero-copy.md))
- [Web Crypto API and SubtleCrypto Lab](https://frontend.rocks/concepts/web-crypto-subtle/#lab) ([Markdown](https://frontend.rocks/concepts/web-crypto-subtle.md))

## Related Architecture Autopsies
- [Autopsy #12: Moving the Entire App Engine Off the Main Thread](https://frontend.rocks/autopsies/zero-main-thread-ui-worker/) ([Markdown](https://frontend.rocks/autopsies/zero-main-thread-ui-worker.md)): Rich spreadsheet and graph app drops to 4 FPS during 100k-row sorts until state and OPFS move to a Worker.

## Primary Sources
- https://tc39.es/ecma262/#sec-typedarray-objects
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays
- https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto
- https://w3c.github.io/webcrypto/
