liveBook · Manning

JavaScriptCore

As explained in JavaScript in Depth

JSC, or JavaScriptCore, is a JavaScript engine used to execute JavaScript and manage its memory.

JavaScriptCore (JSC) is the JavaScript engine used by Safari, all iOS browsers, and Bun. Its Riptide garbage collector takes a different path from V8: objects never move, and much of the collector's design follows from that constraint. section 9.3

Riptide performs most marking on background threads while application code continues. Write barriers can force the collector to revisit old objects; sustained mutation may create a death spiral. A space-time scheduler responds by spending progressively more CPU on collection as allocations consume headroom, eventually pausing the application if necessary. Eden collections usually complete with pauses under a millisecond. section 9.3.4

JSC also avoids eager sweeping through logical versioning. A global version invalidates block metadata lazily, so dead blocks are reclaimed when allocation or marking needs them. This can delay cleanup of native resources attached to dead JavaScript objects, although a background incremental sweeper mitigates the delay. section 9.3.5

The old-object mutation trap

An in-memory sessions Map can retain long-lived session objects while each request assigns freshly allocated request data, headers, and history arrays. This combination of allocation and old-generation mutation can increase revisit work and trigger throttling in JSC. section 9.3.4

const sessions = new Map();  #A
function handleRequest(req) {
  let session = sessions.get(req.sessionId);
  if (!session) {
    session = { id: req.sessionId, created: Date.now() };
    sessions.set(req.sessionId, session);
  }
 
  // Every request mutates the long-lived session object
  // with fresh short-lived objects
  session.lastAccess = Date.now();
  session.lastRequest = {  #B
    url: req.url,
    headers: { ...req.headers },
    timestamp: Date.now(),
  };
  session.history = (session.history || [])
    .slice(-99)
    .concat([req.url]);  #C
 
  return buildResponse(req, session);
}

Trace the request handler's long-lived Map, repeated session mutation, and fresh per-request allocations. section 9.3.4

Use these rules when reasoning about JSC memory behavior:

  • Control reachability rather than merely counting allocations: short-lived objects are cheaper than objects that survive. section 9.7
  • Expect diagnostics to differ from V8: Bun provides CPU profiling and heap snapshots, but not all V8 garbage-collection traces or metrics. section 11.6

How JavaScript in Depth covers it

How deep it goes
Descends into non-moving collection, concurrent marking, death-spiral prevention, logical versioning, and diagnostic limitations.
Examples
Uses a request-session cache, native resources, heap snapshots, allocation timelines, and Bun debugging as concrete cases.
What it assumes
Assumes JavaScript programs and memory behavior, while explaining collector tradeoffs without requiring prior garbage-collection implementation knowledge.
What you can do after
Diagnose JSC memory pressure by recognizing old-object mutation, allocation survival, heap retention, and runtime-specific profiling signals.
How it connects
Connects garbage collection with generational memory, concurrent marking, write barriers, lazy sweeping, logical versioning, reachability, and heap snapshots.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.