it would be nice if I could just ask V8 or Libuv or whatever
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 member type
(that has type uv_handle_type
) is equal to UV_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:
struct Counter {
static int count;
static void callback(uv_handle_t* handle, void*) {
if(handle.type == uv_handle_type::UV_TIMER) count++;
}
};
You can use it as it follows:
Counter::count = 0;
uv_walk(my_loop_ptr, &Counter::callback);
// Counter::count indicates how many active timers are running on the loop
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.