在常见浏览器(Chrome,FF,IE)中实现setTimeout
和clearTimeout
的算法是什么?
首先想到的算法是可以在最小堆上维护超时,按运行时间排序。首先从堆中删除超时。
1
/ \
2 7
/ \
5 5
(where numbers are the UTC time, or something)
这将使asympotic performance
setTimeout - O(log n)
clearTimeout - O(n)
setTimeout
和clearTimeout
的效果之间是否有这样的差异,还是使用了不同的算法? (例如,使用堆,但使用“禁用”标记标记超时,而不是立即从堆中查找和删除它们。)