Skip to content

side-projects-overkill/fast-api-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Node.js Performance Paradox

Understanding I/O Strength vs CPU Weakness β€” and How to Solve It

This project provides interactive JavaScript examples demonstrating the Node.js performance paradox and its solutions through Worker Threads and Native Modules.

πŸ“š What You'll Learn

  1. The Paradox: Why Node.js excels at I/O but struggles with CPU tasks
  2. Event Loop Deep Dive: Understand every phase (nextTick, Promise, timers, I/O, setImmediate)
  3. Worker Threads: Offloading CPU work to parallel JavaScript threads
  4. Worker Pools: Production-ready patterns with task queues
  5. Native Modules: Maximum performance with Rust/C++ integration
  6. WebAssembly: Portable, sandboxed alternative to native addons
  7. Ecosystem Comparison: When to choose Native vs WASM
  8. Ultimate Architecture: Main Thread β†’ Worker Pool β†’ Native Core

πŸš€ Quick Start

# START HERE - The Ultimate Showdown
npm start                   # Watch I/O vs CPU in real-time!

# Or run demos individually
npm run demo:showdown       # 0. I/O vs CPU showdown with live requests
npm run demo:deep-dive      # 1. Event loop phases explained
npm run demo:blocking       # 2. See the event loop blocking problem
npm run demo:worker-threads # 3. Solution A: Worker Threads
npm run demo:worker-pool    # 4. Production Worker Pool pattern
npm run demo:native-addon   # 5. Solution B: Native Modules
npm run demo:wasm           # 6. WebAssembly demos
npm run demo:ecosystem      # 7. Native vs WASM comparison
npm run demo:architecture   # 8. The Ultimate Architecture

# Run benchmarks
npm run benchmark

# Open interactive visualization
npm run visualize

πŸ“ Project Structure

β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ 00-io-vs-cpu-showdown.js     # ⭐ THE showcase - I/O vs CPU with live requests
β”‚   β”œβ”€β”€ 01-event-loop-deep-dive.js   # Event loop phases explained
β”‚   β”œβ”€β”€ 01-blocking-event-loop.js    # The problem: CPU blocks event loop
β”‚   β”œβ”€β”€ 02-worker-threads-solution.js # Solution A: Worker Threads
β”‚   β”œβ”€β”€ 03-worker-pool.js            # Production Worker Pool pattern
β”‚   β”œβ”€β”€ 04-native-addon-demo.js      # Solution B: Native Modules
β”‚   β”œβ”€β”€ 05-wasm-demo.js              # WebAssembly alternative
β”‚   β”œβ”€β”€ 06-ecosystem-comparison.js   # Native vs WASM comparison
β”‚   └── 07-ultimate-architecture.js  # Complete architecture demo
β”œβ”€β”€ benchmarks/
β”‚   └── run-all.js                   # Performance benchmark suite
β”œβ”€β”€ visualization/
β”‚   └── index.html                   # Interactive web visualization
└── package.json

🎯 The Node.js Paradox

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         EVENT LOOP                                  β”‚
β”‚                                                                     β”‚
β”‚    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚    β”‚  Timer   │────▢│   I/O    │────▢│  Check   │────▢│  Close   β”‚ β”‚
β”‚    β”‚ Callbacksβ”‚     β”‚ Callbacksβ”‚     β”‚ (setImm) β”‚     β”‚ Callbacksβ”‚ β”‚
β”‚    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚          β–²                                                    β”‚    β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

βœ… STRENGTH: Non-blocking I/O (network, disk, database)
❌ WEAKNESS: CPU-bound tasks BLOCK the entire loop

πŸ’‘ Solutions

Solution A: Worker Threads

import { Worker, isMainThread, parentPort } from 'worker_threads';

// Main thread stays free for I/O
const worker = new Worker('./cpu-task.js');
worker.postMessage({ data: heavyData });
worker.on('message', result => {
  // CPU work done in background!
});

Solution B: Native Modules

// Using sharp (native C++ image processing)
import sharp from 'sharp';

await sharp('photo.jpg')
  .resize(800, 600)
  .webp({ quality: 85 })
  .toFile('output.webp');
// 50x faster than pure JavaScript!

πŸ“Š Comparison Matrix

Criteria Native Addons WebAssembly
⚑ Performance 100% (Maximum) 70-90% (Near-native)
🌍 Portability 35% (Per-platform) 100% (Universal)
πŸ”’ Safety 50% (Full OS access) 100% (Sandboxed)
πŸ”§ OS Access 100% (Full) 15% (Limited/WASI)
🌐 Browser 0% (Node only) 100% (Everywhere)

πŸ—οΈ Ultimate Architecture

                    INCOMING REQUESTS
                          β”‚
                          β–Ό
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃              MAIN THREAD (Event Loop)                  ┃
┃   HTTP Routing β”‚ WebSocket Management β”‚ Response       ┃
┃   ⚑ MUST STAY LIGHTWEIGHT - No CPU work here!         ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
                           β”‚
              postMessage() / IPC
                           β”‚
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                    WORKER POOL                         ┃
┃  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”          ┃
┃  β”‚Worker 1β”‚ β”‚Worker 2β”‚ β”‚Worker 3β”‚ β”‚Worker Nβ”‚  Γ— CPUs  ┃
┃  β”‚        β”‚ β”‚        β”‚ β”‚        β”‚ β”‚        β”‚          ┃
┃  β”‚ β”Œβ”€β”€β”€β”€β” β”‚ β”‚ β”Œβ”€β”€β”€β”€β” β”‚ β”‚ β”Œβ”€β”€β”€β”€β” β”‚ β”‚ β”Œβ”€β”€β”€β”€β” β”‚          ┃
┃  β”‚ β”‚RUSTβ”‚ β”‚ β”‚ β”‚RUSTβ”‚ β”‚ β”‚ β”‚RUSTβ”‚ β”‚ β”‚ β”‚RUSTβ”‚ β”‚          ┃
┃  β”‚ β”‚ πŸš€ β”‚ β”‚ β”‚ β”‚ πŸš€ β”‚ β”‚ β”‚ β”‚ πŸš€ β”‚ β”‚ β”‚ β”‚ πŸš€ β”‚ β”‚          ┃
┃  β”‚ β””β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”˜ β”‚ β”‚ β””β”€β”€β”€β”€β”˜ β”‚          ┃
┃  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜          ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

πŸ“¦ Real-World Libraries

Native Addons (Maximum Speed)

  • sharp - Image processing (libvips)
  • bcrypt - Password hashing
  • better-sqlite3 - SQLite database
  • node-canvas - 2D graphics

WebAssembly (Portable)

  • @ffmpeg/ffmpeg - Video processing
  • @duckdb/duckdb-wasm - Analytics database
  • esbuild-wasm - JavaScript bundler
  • sql.js - SQLite in WASM

πŸ”§ Building Native Modules

With Rust (napi-rs)

use napi_derive::napi;

#[napi]
pub fn count_primes(max: u32) -> u32 {
    (2..=max).filter(|&n| is_prime(n)).count() as u32
}

With C++ (node-addon-api)

Napi::Number CountPrimes(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();
    uint32_t max = info[0].As<Napi::Number>().Uint32Value();
    return Napi::Number::New(env, count_primes(max));
}

πŸ“ˆ Benchmark Results

Run npm run benchmark to see results on your machine:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       METHOD        β”‚  TOTAL TIME  β”‚   SPEEDUP   β”‚   EVENT LOOP        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ ❌ Blocking         β”‚    2000ms    β”‚    1.0x     β”‚ β›” BLOCKED          β”‚
β”‚ βœ… Worker Threads   β”‚     600ms    β”‚    3.3x     β”‚ βœ… FREE             β”‚
β”‚ πŸš€ Native (est.)    β”‚      80ms    β”‚   25.0x     β”‚ βœ… FREE             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“– Further Reading

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published