Skip to content

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.

License

Notifications You must be signed in to change notification settings

C-W-D-Harshit/JS-Event-Loop-Sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

JS-Event-Loop-Sim

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.

License TypeScript React

โœจ Features

๐ŸŽฏ Interactive Visualization

  • 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

๐ŸŽฎ Playback Controls

  • 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

๐Ÿ“š Educational Scenarios

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

๐Ÿ”ฌ Interactive Experimentation

  • 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

๐ŸŽจ Modern UI/UX

  • 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

๐Ÿš€ Getting Started

Prerequisites

  • Bun 1.2.22 or higher

Installation

# 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

Development

# Start the development server
bun run dev

# The application will be available at http://localhost:3001

Build

# Build for production
bun run build

# Preview production build
bun run serve

Type Checking

# Check TypeScript types across all apps
bun run check-types

๐Ÿ“– Available Scenarios

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

๐Ÿ—๏ธ Project Structure

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

๐Ÿ› ๏ธ Technology Stack

Frontend

  • 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

Development

  • Vite - Build tool and dev server
  • Bun - Package manager and runtime
  • PrismJS - Syntax highlighting
  • React Resizable Panels - Layout management

State Management

  • useReducer - Local state with custom simulator reducer
  • React Context - Global simulator state sharing

๐ŸŽ“ How It Works

The simulator uses a reducer-based state machine that models the JavaScript event loop:

  1. Task Queues: Maintains separate queues for microtasks, macrotasks, and Node.js nextTick callbacks
  2. Call Stack: Tracks function execution frames with source line references
  3. Web APIs: Simulates async operations (setTimeout, fetch, etc.) with progress tracking
  4. Phase Tracking: Models event loop phases for both browser and Node.js runtimes
  5. 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.

๐ŸŽฏ Use Cases

  • 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

๐Ÿค Contributing

Contributions are welcome! Feel free to:

  • Add new scenarios
  • Improve visualizations
  • Fix bugs
  • Enhance documentation

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

Built with Better-T-Stack

๐Ÿ“ฌ Contact

For questions, suggestions, or feedback, please open an issue on GitHub.


Happy Learning! ๐Ÿš€

About

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.

Topics

Resources

License

Stars

Watchers

Forks