Is there a way to make a call in Node.js to determine the number of timers in the event loop queue? I have a library with a number of timeouts and instead of keeping track of them myself using some sort of internal bookkeeping system, it would be nice if I could just ask V8 or Libuv or whatever, how many timers there are.
Is this possible?
You cannot directly ask libuv, but it certainly offers a way to know how many active timers are there.
To do that, you can invoke
uv_walk
with a valid loop to get all the active handles. Then you can check each handle with the given callback and count those for which the data membertype
(that has typeuv_handle_type
) is equal toUV_TIMER
.The result is the number of active timers.
See the documentation for further details about the handle data structure.
As a trivial example, consider the following structure:
You can use it as it follows:
Of course, this is not a production-ready code. Anyway, I hope it gives an idea of the proposed solution.
See here for the libuv documentation.