An interactive, step-by-step JavaScript Event Loop visualizer that helps developers understand how asynchronous code execution works in both browser and Node.js environments.
- Real-time Event Loop Simulation: Watch the call stack, microtask queue, macrotask queue, and Web APIs interact step-by-step
- Dual Runtime Support: Switch between Browser and Node.js event loop models to see runtime-specific behaviors
- Color-Coded Components: Distinct colors for different task types (microtasks in violet, macrotasks in amber, etc.)
- Smooth Animations: Framer Motion-powered transitions for intuitive understanding of task flow
- Step-by-Step Execution: Move forward/backward through code execution at your own pace
- Variable Speed Playback: Adjust speed from 0.5x to 4x for different learning preferences
- Timeline Navigation: Jump to specific execution steps instantly
- Play/Pause/Reset: Full VCR-style controls for exploration
10 carefully crafted scenarios covering:
- Fundamentals: Basic sync execution, setTimeout vs Promise, async/await patterns
- Browser-Specific: Render pipeline, requestAnimationFrame
- Node.js-Specific: process.nextTick, setImmediate, event loop phases
- Tricky Cases: Microtask starvation, promise executor behavior, nested timeouts
Each scenario includes:
- Syntax-highlighted code with real-time line highlighting
- Expected output for verification
- Detailed explanations of event loop behavior
- Step-by-step execution plan
- Manual Task Enqueueing: Add custom tasks (promises, timeouts, etc.) during execution
- Runtime Switching: Toggle between browser and Node.js to compare behaviors
- Queue Inspection: See tasks waiting in microtask, macrotask, and nextTick queues
- Web API Progress: Watch async operations complete with progress indicators
- Resizable Panels: Customize your workspace layout on desktop
- Responsive Design: Fully functional on mobile and tablet devices
- Dark/Light Mode: Theme support for comfortable viewing
- Console Output: Simulated console with animated log entries
- Bun 1.2.22 or higher
# Clone the repository
git clone https://github.com/yourusername/js-event-loop-sim.git
# Navigate to the project directory
cd js-event-loop-sim
# Install dependencies
bun install# Start the development server
bun run dev
# The application will be available at http://localhost:3001# Build for production
bun run build
# Preview production build
bun run serve# Check TypeScript types across all apps
bun run check-types| Scenario | Category | Runtime | Description |
|---|---|---|---|
| Hello Sync | Fundamentals | Both | Basic synchronous execution |
| setTimeout vs Promise | Fundamentals | Browser | The classic microtask vs macrotask question |
| async/await Basics | Fundamentals | Both | How async/await desugars to Promises |
| Nested setTimeout | Fundamentals | Browser | One macrotask per event loop iteration |
| Microtask Chain | Fundamentals | Both | Microtasks can queue more microtasks |
| nextTick vs Promise | Node.js | Node | process.nextTick runs before Promise.then |
| Promise inside setTimeout | Tricky | Browser | Microtasks drain after each macrotask |
| queueMicrotask API | Fundamentals | Both | Using queueMicrotask directly |
| Multiple Awaits | Tricky | Both | Each await creates a microtask checkpoint |
| Promise Executor is Sync | Tricky | Both | The Promise constructor callback runs synchronously |
| setImmediate vs setTimeout | Node.js | Node | Check phase vs timer phase ordering |
| Mixed Async Patterns | Tricky | Browser | Classic interview question combining all patterns |
| Microtask Starvation | Tricky | Both | Microtasks can starve the macrotask queue |
js-event-loop-sim/
โโโ apps/
โ โโโ web/ # React frontend application
โ โโโ src/
โ โ โโโ components/
โ โ โ โโโ simulator/ # Simulator components
โ โ โ โ โโโ CallStack.tsx
โ โ โ โ โโโ CodePanel.tsx
โ โ โ โ โโโ ConsolePanel.tsx
โ โ โ โ โโโ Controls.tsx
โ โ โ โ โโโ PhaseIndicator.tsx
โ โ โ โ โโโ Queues.tsx
โ โ โ โ โโโ QuickEnqueue.tsx
โ โ โ โ โโโ ScenarioSelector.tsx
โ โ โ โ โโโ SimulatorProvider.tsx
โ โ โ โ โโโ TaskCard.tsx
โ โ โ โ โโโ WebApis.tsx
โ โ โ โโโ ui/ # shadcn/ui components
โ โ โโโ hooks/ # Custom React hooks
โ โ โโโ lib/
โ โ โ โโโ simulator/ # Core simulator logic
โ โ โ โโโ engine.ts # Reducer and state management
โ โ โ โโโ scenarios.ts # Predefined scenarios
โ โ โ โโโ types.ts # TypeScript definitions
โ โ โโโ routes/ # TanStack Router routes
โ โโโ package.json
โโโ packages/
โ โโโ config/ # Shared TypeScript config
โ โโโ env/ # Environment variable handling
โโโ package.json
- React 19 - UI library
- TypeScript 5 - Type safety
- TanStack Router - File-based routing with type safety
- TailwindCSS 4 - Utility-first styling
- shadcn/ui - Reusable component library
- Framer Motion - Animation library
- Vite - Build tool and dev server
- Bun - Package manager and runtime
- PrismJS - Syntax highlighting
- React Resizable Panels - Layout management
- useReducer - Local state with custom simulator reducer
- React Context - Global simulator state sharing
The simulator uses a reducer-based state machine that models the JavaScript event loop:
- Task Queues: Maintains separate queues for microtasks, macrotasks, and Node.js nextTick callbacks
- Call Stack: Tracks function execution frames with source line references
- Web APIs: Simulates async operations (setTimeout, fetch, etc.) with progress tracking
- Phase Tracking: Models event loop phases for both browser and Node.js runtimes
- Event Logging: Records all state transitions for timeline navigation
Each scenario contains predefined execution steps that simulate the exact sequence of operations the JavaScript engine would perform.
- Learning: Understand JavaScript's asynchronous execution model visually
- Teaching: Demonstrate event loop concepts to students with interactive examples
- Interview Prep: Master common async JavaScript interview questions
- Debugging: Develop intuition for async code behavior and timing issues
- Comparison: See concrete differences between browser and Node.js event loops
Contributions are welcome! Feel free to:
- Add new scenarios
- Improve visualizations
- Fix bugs
- Enhance documentation
This project is licensed under the MIT License - see the LICENSE file for details.
Built with Better-T-Stack
For questions, suggestions, or feedback, please open an issue on GitHub.
Happy Learning! ๐