Created
November 19, 2017 16:47
-
-
Save SeanSobey/94495ac7ffb88380ab1ab2eeb3773fdc to your computer and use it in GitHub Desktop.
Debug node timer leaks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const activeTimeoutIds = new Map(); | |
const _setTimeout = setTimeout; | |
setTimeout = function (callback, ms, ...args) { | |
const timeoutId = _setTimeout(() => { | |
callback(); | |
activeTimeoutIds.delete(timeoutId); | |
}, ms, ...args); | |
const stackTrace = (new Error()).stack; | |
activeTimeoutIds.set(timeoutId, 'ms: ' + ms + ', trace:' + stackTrace); | |
return timeoutId; | |
}; | |
const _clearTimeout = clearTimeout; | |
clearTimeout = function (timeoutId) { | |
_clearTimeout(timeoutId); | |
activeTimeoutIds.delete(timeoutId); | |
} | |
function getTimeouts() { | |
return Array.from(activeTimeoutIds.values()); | |
} | |
const _setInterval = setInterval; | |
const activeIntervalIds = new Map(); | |
setInterval = function (callback, ms, ...args) { | |
const timeoutId = _setInterval(callback, ms, ...args); | |
const stackTrace = (new Error()).stack; | |
activeIntervalIds.set(timeoutId, 'ms: ' + ms + ', trace:' + stackTrace); | |
return timeoutId; | |
}; | |
const _clearInterval = clearInterval; | |
clearInterval = function (timeoutId) { | |
_clearInterval(timeoutId); | |
activeIntervalIds.delete(timeoutId); | |
} | |
function getIntervals() { | |
return Array.from(activeIntervalIds.values()); | |
} | |
console.log('timeouts', Array.from(getTimeouts())); | |
console.log('intervals', Array.from(getIntervals())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment