0Pricing
WebAssembly (WASM) for High Performance Apps · Lesson

Sharing Memory and Typed Arrays Across the JS/WASM Boundary

Master efficient data exchange between JavaScript and WebAssembly using linear memory, typed array views, and pointer-based ABIs.

Sharing Memory and Typed Arrays Across the JS/WASM Boundary is a free WebAssembly (WASM) for High Performance Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebAssembly (WASM) for High Performance Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Boundary Problem

WASM functions only accept and return numbers. To pass strings, arrays, or structs you must work directly with WebAssembly.Memory — a single growable ArrayBuffer.

Linear Memory as a View

You read and write guest memory from JS by creating typed array views over the module memory buffer.

const mem = wasm.exports.memory;
const u8 = new Uint8Array(mem.buffer);
u8[0] = 65; // write a byte at offset 0

Allocating Inside WASM

The guest owns its heap, so allocate there and get back a pointer.

const ptr = wasm.exports.malloc(len);
const view = new Uint8Array(wasm.exports.memory.buffer, ptr, len);
view.set(bytes);
wasm.exports.process(ptr, len);
wasm.exports.free(ptr);

Passing Strings In

Encode a JS string to UTF-8 bytes, copy into guest memory, pass pointer + length.

const enc = new TextEncoder();
const bytes = enc.encode("hello");
const ptr = wasm.exports.malloc(bytes.length);
new Uint8Array(wasm.exports.memory.buffer, ptr, bytes.length).set(bytes);
wasm.exports.handle(ptr, bytes.length);

Reading Strings Out

Given a pointer + length returned by the guest, slice the bytes and decode.

const u8 = new Uint8Array(wasm.exports.memory.buffer, ptr, len);
const str = new TextDecoder().decode(u8);

The Detached Buffer Trap

When WASM memory grows, the old ArrayBuffer is detached and your views become invalid. Always recreate views after a call that may allocate.

Numeric Arrays

For Float64Array or Int32Array data, use the matching view and remember offsets are in bytes, so multiply the index by the element size.

const f64 = new Float64Array(wasm.exports.memory.buffer, ptr, count);

Zero-Copy with SharedArrayBuffer

If both JS and WASM use a SharedArrayBuffer-backed memory, multiple threads can read the same data without copying — powerful but requires atomics for safety.

Binding Generators

Tools like wasm-bindgen (Rust) or Emscripten embind hide this pointer arithmetic, generating glue that marshals strings and objects automatically.

Endianness & Alignment

WASM memory is little-endian. Respect type alignment (e.g. 8-byte boundaries for f64) when laying out structs manually to avoid corrupt reads.

Ownership Rules

Decide who frees what. A common convention: the side that allocates also frees. Leaking guest allocations grows linear memory until memory.grow fails.

Quick Check

Why can a typed array view become invalid after calling a WASM function?

Recap

You learned to exchange data across the boundary via linear memory, pointer + length ABIs, and typed-array views. Watch the detached-buffer trap after growth, respect alignment, define clear ownership, and lean on binding generators for ergonomics.

Frequently asked questions

Is the “Sharing Memory and Typed Arrays Across the JS/WASM Boundary” lesson free?

Yes — the full text of “Sharing Memory and Typed Arrays Across the JS/WASM Boundary” is free to read here on the web, and the WebAssembly (WASM) for High Performance Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebAssembly (WASM) for High Performance Apps course, upgrade to CoddyKit PRO.

What will I learn in “Sharing Memory and Typed Arrays Across the JS/WASM Boundary”?

Master efficient data exchange between JavaScript and WebAssembly using linear memory, typed array views, and pointer-based ABIs. You practise WebAssembly (WASM) for High Performance Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start WebAssembly (WASM) for High Performance Apps?

No prior experience is required. WebAssembly (WASM) for High Performance Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sharing Memory and Typed Arrays Across the JS/WASM Boundary” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this WebAssembly (WASM) for High Performance Apps lesson?

Yes. Every WebAssembly (WASM) for High Performance Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Asynchronous Operations with WASM
  2. Custom JavaScript Callbacks
  3. Error Handling & Exceptions
  4. Sharing Memory and Typed Arrays Across the JS/WASM Boundary
← Back to WebAssembly (WASM) for High Performance Apps