What is the algorithm implementation setTimeout, and clearTimeoutin common browsers (Chrome, FF, IE)?
The algorithm that first comes to mind is that timeouts can be maintained on the minimum heap, sorted by time to run. Timeouts are first removed from the heap.
1
/ \
2 7
/ \
5 5
(where numbers are the UTC time, or something)
This will make asymptotic performance
setTimeout - O(log n)
clearTimeout - O(n)
Is there a difference between performance setTimeoutand clearTimeoutis another algorithm used? (For example, using a heap, but noting timeouts with the flag turned off, rather than instantly detecting and removing them from the heap.)
source
share