← Back to Node.js, page 9

nodejs-0176 · Easy fresher

What is an edge runtime (Cloudflare Workers, Vercel Edge), and how does it differ from Node.js?

Edge runtimes run small functions in many locations close to users, usually in V8 isolates with Web-standard APIs (fetch, Request, Response, URL, crypto.subtle) and strict limits on CPU time, memory and bundle size. They start in milliseconds and have no file system, child processes or most Node core modules (some compatibility exists). Node gives the full platform and long-running processes. Edge is good for routing, auth checks, redirects and personalisation, and Node for heavy or stateful work.

Full answer

How edge runtimes work

  • Code runs in V8 isolates (lightweight sandboxes) instead of full processes or containers. Many isolates share one process. Start-up takes milliseconds, so cold starts are nearly invisible.
  • The programming model is the Web platform: export default { fetch(request, env, ctx) { return new Response('ok') } } (Workers style), or a (Request) => Response function. APIs available: fetch, Request, Response, Headers, URL, URLSearchParams, crypto.subtle, TextEncoder, streams, and often KV or object storage bindings.
  • Limits: CPU time per request (often 10 to 50 ms of CPU), memory around 128 MB, bundle size limits, no long-lived background tasks, and limited or no TCP connections (some now support them).

Compared with Node.js

  • Node: full standard library (fs, net, child_process, worker_threads), native addons, any npm package, long-running servers, and stateful processes.
  • Edge: no fs, no child processes, no native addons, only packages that use Web APIs. Many Node APIs exist through compatibility flags (nodejs_compat) but not all.
  • Data access: databases are usually far from the edge, so a round trip from the edge to a central database can cost more than it saves. Use edge-friendly data (KV, replicated databases, HTTP data APIs) or cache.

When to use which

  • Edge: redirects, A/B tests, authentication checks, header rewriting, geolocation, caching and simple APIs where latency matters.
  • Node: CPU-heavy tasks, long connections, large dependencies, anything that needs the file system or native libraries.

Portable style: write handlers with Web-standard Request and Response, so they run on Node, Deno, Bun and edge platforms.

javascript
// A handler written only with Web-standard APIs. The same function body would run on an edge runtime.
async function handle(request) {
  const url = new URL(request.url);

  if (url.pathname === '/hello') {
    const name = url.searchParams.get('name') ?? 'world';
    return Response.json({ message: `Hello ${name}`, country: request.headers.get('x-country') ?? 'unknown' });
  }
  if (url.pathname === '/old') return Response.redirect(new URL('/hello', url).href, 301);
  if (url.pathname === '/hash') {
    const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(url.searchParams.get('text') ?? ''));
    return new Response(Buffer.from(digest).toString('hex').slice(0, 16), { headers: { 'content-type': 'text/plain', 'cache-control': 'public, max-age=60' } });
  }
  return new Response('not found', { status: 404 });
}

(async () => {
  const show = async (label, req) => {
    const res = await handle(req);
    const body = res.status === 301 ? res.headers.get('location') : await res.text();
    console.log(label.padEnd(10), res.status, body, res.headers.get('cache-control') ?? '');
  };
  await show('hello', new Request('https://example.com/hello?name=Asha', { headers: { 'x-country': 'IN' } }));
  await show('redirect', new Request('https://example.com/old'));
  await show('hash', new Request('https://example.com/hash?text=edge'));
  await show('missing', new Request('https://example.com/nothing'));

  // Web APIs that exist in Node 24 as globals, so the handler above runs here unchanged.
  const globals = ['fetch', 'Request', 'Response', 'Headers', 'URL', 'crypto', 'ReadableStream', 'TextEncoder'];
  console.log('available as globals in Node:', globals.every((name) => name in globalThis));
})();

The handler uses only Web-standard APIs and is called with constructed Request objects. This shows the portable style, not the edge platform: an edge runtime was not available here, so the limits are described in the text only. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): hello 200 {"message":"Hello Asha","country":"IN"} redirect 301 https://example.com/hello hash 200 a1cb100f57e971ca public, max-age=60 missing 404 not found available as globals in Node: true

Key points

  • Edge runtimes use isolates and Web-standard APIs.
  • They start fast but have strict CPU, memory and size limits.
  • No file system, child processes or native addons.
  • Data far from the edge can erase the latency gain.
  • Write portable handlers with Request and Response.

Common mistakes

  • Assuming every npm package works at the edge.
  • Calling a distant central database from many edge locations.
  • Doing CPU-heavy work in an edge function.
  • Using Node-only APIs such as fs in code meant for the edge.
  • Choosing edge for everything because it is fast to start.

Follow-ups

  • What is a V8 isolate, and how does it differ from a container?
  • How do you share code between Node and edge handlers?
  • What does nodejs_compat do on Workers?

Tips

  • Contrast isolates with processes.
  • List what is missing at the edge.
  • Name use cases for each.