liveBook · Manning

Synchronous call

As explained in JavaScript in Depth

Execution in which JavaScript work runs to completion on the current thread before later work can proceed.

A synchronous callback can delay unrelated work: while it occupies the main thread, other callbacks wait, making I/O appear slow. In a stream transform, genuinely synchronous work need not be wrapped in an async function; doing so adds promise and microtask overhead without useful I/O waiting. section 10.7.1 section 11.1.9

Use the observed delay to guide the investigation:

  • If event loop delay is high, check for GC pauses; if GC is not responsible, inspect synchronous JavaScript with a CPU profile. section 11.3.1
  • Keep CPU-bound transform work synchronous when possible; reserve async transforms for work that genuinely waits on I/O. section 10.7.1
  • When I/O latency rises, compare it with event loop delay to distinguish local blocking from slow network or remote service behavior. section 11.1.9
const transform = new stream.Transform({
  transform(chunk, encoding, callback) {
    callback(null, processChunk(chunk));
  }
});

A Node.js transform callback processes a chunk synchronously without creating a promise. section 10.7.1

// Web Streams
const transform = new TransformStream({
  async transform(chunk, controller) {
    const result = processChunk(chunk);  // #A
    controller.enqueue(result);
  }
});

A Web Streams transform uses an async method even when its chunk-processing work is synchronous. section 10.7.1

How JavaScript in Depth covers it

How deep it goes
Explains promise creation, microtask scheduling, and event-loop effects, then uses profiling to separate causes of delay.
Examples
Contrasts Node.js and Web Streams transforms and compares I/O latency with event loop delay.
What you can do after
Diagnose event-loop stalls by checking GC, profiling synchronous JavaScript, and comparing I/O latency with event loop delay.
How it connects
Connects synchronous calls with promises, microtasks, backpressure, garbage collection, event loop delay, and I/O latency.

Also explained in

Compare how 12 books explain synchronous call →

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.