r/learnjavascript • u/Alternative-Host-210 • 5d ago
Can one slow Express route make a different route slow too?
I’m turning a workshop booking API idea into a few small Node experiments. Before adding MongoDB, I want to understand what happens when two requests hit the same server.
Imagine these two routes in an Express app:
app.;
app.;
The loop is deliberately bad code. My prediction is that if /quick arrives while the loop is running, it has to wait. If I replace the loop with setTimeout(() => res.send('done'), 2000), I’d expect /quick to respond while /slow is still waiting.
What’s the simplest way to test this with two overlapping requests and make the difference obvious to someone learning Node?
3
u/tachyon_bit 5d ago
Your prediction is correct. The synchronous while loop blocks Node's event loop, so /quick has to wait. setTimeout schedules the callback and gives control back to the event loop, allowing /quick to execute immediately.
The distinction is blocking vs non-blocking execution, not just slow vs fast routes.
3
u/ajamdonut 5d ago
everyone here assuming express handles all requests in one thread rather than spawning children which have completely seperate runtime env.
1
u/chikamakaleyley helpful 5d ago
i don't think express or a server even matters;
you can just write as normal functions that console log some text, execute a long list of them, and observe the behavior, right?
obviously you're trying to see what happens if the server receives them 'at the same time' but when you're using like 2 sec of delay that's negligible, i think
1
u/cuelark 5d ago
easiest way is to start the server and fire /slow with curl in one terminal, then immediately curl /quick in a second one with
curl -w '%{time_total}\n' localhost:3000/quick. with the busy loop /quick will report about 2 seconds, with setTimeout it comes back in a few ms. that time_total number makes the difference really obvious, and a browser can confuse you here because it queues requests to the same url sometimes1
u/Content-Fee-4115 5d ago
Your test is actually superb. JavaScript is single threaded. For your first case, if you hit slow first then hit quick second,,the execution of the slow endpoint blocks the thread of execution, thus the handler for the quick endpoint gets added to the poll queue, and it would have to wait until the thread finishes executing the slow endpoint
For the second case, using setTimeout differs the slow work in node, as the timer runs, the request to quick arrives and gets added to the poll queue, and since the thread is free, it executes it. once the 2 second timer elapses, the callback function gets added to the timer queue, and since the thread aint blocked, it gets executed.
You need to learn more about the event loop, the callstack and the queues so that you be able to know when to defer slow functionality like the one you provided as an exampe behind the queues, so that the other faster endpoints dont get deprived.
1
u/sereikis 4d ago
for the app shown, Express doesn't spawn a child process per request: both handlers run on the same event loop. if you add cluster workers or multiple instances later, keep this demo to one worker so the requests cannot land in different processes.
1
u/TheRNGuy 2d ago
Open both links in 2 tabs while browser networks tab is open.