Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does setTimeout function affect the performance of Node.js application?

I have a request handler for ticket booking:

route.post('/makeBooking', (req, res) => {
  // Booking code

  setTimeout(function () {
    // Checks if the payment is made, if not then cancels the booking
  }, 900000);
});

Now I've a route which makes a booking and if the payment is not made within 15 minutes the timeout function will cancel the booking.

Will this function cause any performance related issues or memory leaks?

like image 769
Sushant K Avatar asked May 30 '26 02:05

Sushant K


1 Answers

Will this function cause any performance related issues ...

No it won't, at least not in and by itself. While setTimeout is waiting to invoke it's callback, it's non-blocking. The call is simply added to a queue. At some point in the future the callback fires and the call is removed from that queue.

In the meantime, you can still process stuff.

... or memory leaks?

The setTimeout callback is within a closure. As soon as setTimeout invokes the callback, it becomes eligible for garbage collection.

Unless you get many millions of bookings within the 900000ms timeframe, you have nothing to worry about; the number of course depends on the memory size you allocated to your Node.js application.

Of course if you do get that many requests-per-second, you have other, more important stuff to worry about.

like image 82
nicholaswmin Avatar answered Jun 01 '26 15:06

nicholaswmin