A comprehensive visual tool to understand and demonstrate JavaScript's event loop mechanism with real-time monitoring and interactive examples.
This project provides an interactive visualization of how JavaScript's event loop works, including the call stack, task queue, microtask queue, and Web APIs. It features both server-side demonstrations and real-time WebSocket broadcasting for educational purposes.
- π Real-time Event Loop Monitoring - Live visualization of event loop operations
- π‘ WebSocket Broadcasting - Real-time updates to connected dashboard clients
- π¨ Colored Logging System - Color-coded console output for different operation types
- π§ͺ Interactive Endpoints - Multiple demonstration scenarios
- π Blocking Detection - Detects and reports event loop blocking
- π Race Condition Demos - Shows operation interleaving
- π± Responsive Dashboard - Web-based interface for monitoring
- Node.js (v14 or higher)
- npm or yarn package manager
# Clone the repository
git clone https://github.com/Mishaelx6/Event-loop-visualizer.git
cd Event-loop-visualizer
# Install dependencies
npm install# Start the server
npm start
# For development with auto-restart
npm run devThe server will start on:
- HTTP Server:
http://localhost:3000 - WebSocket Server:
ws://localhost:3001
Event-loop-visualizer/
βββ server.js # Main Express server with WebSocket support
βββ package.json # Dependencies and scripts
βββ package-lock.json # Locked dependency versions
βββ .gitignore # Git ignore rules
βββ README.md # This documentation
βββ public/ # Static web assets
β βββ index.html # Main dashboard HTML
βββ styles/ # CSS stylesheets
β βββ main.css # Main styling
βββ src/ # Client-side JavaScript
βββ main.js # Frontend visualization logic
- **GET
/- Main dashboard with all endpoint links and documentation
GET /blocking/:time- Blocks the event loop for specified milliseconds
- Demonstrates synchronous blocking behavior
- Example:
/blocking/3000(blocks for 3 seconds)
GET /setTimeout/:time- Uses setTimeout for non-blocking delays
- Shows macrotask scheduling
- Example:
/setTimeout/2000
GET /order-of-execution- Demonstrates microtask vs macrotask priority
- Shows process.nextTick, Promise.then, setImmediate, setTimeout order
- Returns JSON with expected execution order
GET /nonblocking-io- Simulates async I/O operations using Promises
- Demonstrates non-blocking behavior
- 2-second simulated I/O delay
GET /complex- Real-world scenario with multiple async operations
- Combines CPU tasks, database queries, and file operations
- Shows coordination of different operation types
GET /race-demo- Demonstrates operation interleaving
- Shows microtasks executing before macrotasks
- Random delays for realistic race conditions
GET /detect-blocking- Monitors event loop health in real-time
- Detects and reports blocking operations
- Shows timing delays and performance issues
The logging system uses color-coded output for different operation types:
| Color | Type | Description |
|---|---|---|
| π΅ Blue | event-loop |
General event loop activity |
| π£ Magenta | microtask |
Promise.then, process.nextTick |
| π‘ Yellow | macrotask |
setTimeout, setImmediate |
| π΄ Red | blocking |
Synchronous blocking operations |
| π Orange | cpu |
CPU-intensive tasks |
| π’ Green | async |
Async operation completion |
| π΅ Cyan | io |
I/O operations |
| π΄ Bright Red | error |
Error conditions and blocking detection |
// Connect to real-time monitoring
const ws = new WebSocket('ws://localhost:3001');
ws.onopen = () => console.log('Connected to event loop monitor');
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log(`${data.timestamp} [${data.type}] ${data.message}`);
};{
"timestamp": "13:25:42.581",
"type": "blocking",
"message": "Starting potentially blocking operation",
"requestId": 1
}# Test blocking behavior
curl http://localhost:3000/blocking/1000
# Test non-blocking behavior
curl http://localhost:3000/setTimeout/1000
# Test complex scenario
curl http://localhost:3000/complex- Open
http://localhost:3000in browser - Open browser console (F12)
- Click on endpoint links
- Observe colored logs and timing
- Connect multiple browser tabs to
ws://localhost:3001 - Make requests to endpoints
- Observe real-time updates in all connected clients
- Monitor for delays >100ms (indicates blocking)
- Watch for microtask queue buildup
- Track macrotask scheduling delays
- Normal: Operations complete within expected timeframes
- Warning: Delays between 100-500ms
- Critical: Delays >500ms indicate severe blocking
{
"start": "node server.js",
"dev": "nodemon server.js",
"build": "webpack --mode production",
"test": "jest"
}app.get('/your-endpoint', (req, res) => {
logWithColour('Your operation', 'type', req.id);
// Your logic here
res.send('Response');
});- Timer Phase - setTimeout, setInterval callbacks
- Pending Callbacks - I/O callbacks
- Idle, Prepare - Internal preparation
- Poll Phase - New I/O events
- Check Phase - setImmediate callbacks
- Close Callbacks - Socket cleanup
- Synchronous Code - Highest priority
- Microtasks - Next after sync code
- Macrotasks - Next event loop iteration
- Callback Hell - Nested callbacks
- Promise Chains - Sequential async operations
- Async/Await - Modern async handling
- Event Emitters - Event-driven architecture
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow existing code style
- Add comprehensive logging
- Test with different scenarios
- Update documentation
This project is licensed under the MIT License - see the LICENSE file for details.
For issues, questions, or contributions:
- Create an issue on GitHub
- Check existing documentation
- Review console logs for debugging
Built with β€οΈ for JavaScript education and event loop understanding