Skip to content

Instantly share code, notes, and snippets.

@raininglemons
Last active November 29, 2018 18:02
Show Gist options
  • Save raininglemons/caf2e9c5c9b32f49b0d22fc06c2a868f to your computer and use it in GitHub Desktop.
Save raininglemons/caf2e9c5c9b32f49b0d22fc06c2a868f to your computer and use it in GitHub Desktop.
const intervals = (() => {
/**
setInterval watcher
**/
const setIntervalCounter = [];
const setIntervalMap = {};
let startTime = Date.now();
let lastTime = startTime;
setInterval(() => {
const now = Date.now();
if (now > (lastTime + 1000)) {
const secondsSinceStart = ((now - startTime) / 1000) >>> 0;
console.info(`I#${secondsSinceStart} - ${setIntervalCounter.length}`);
lastTime = now;
}
}, 1);
const nativeSetInterval = self.setInterval;
const nativeClearInterval = self.clearInterval;
self.setInterval = function() {
const id = nativeSetInterval.apply(this, arguments);
setIntervalCounter.push(id);
setIntervalMap[id] = Array.from(arguments);
return id;
}
self.clearInterval = id => {
const index = setIntervalCounter.indexOf(id);
if (index > -1) {
setIntervalCounter.splice(index, 1);
setIntervalMap[id] = null;
}
return nativeClearInterval.call(this, id);
}
return {
setIntervalCounter,
setIntervalMap
};
})();
(() => {
/**
setTimeout watcher
**/
let setTimeoutCount = 0;
let setTimeoutTotalCount = 0;
let startTime = Date.now();
let lastTime = startTime;
setInterval(() => {
const now = Date.now();
if (now > (lastTime + 1000)) {
const secondsSinceStart = ((now - startTime) / 1000) >>> 0;
console.info(`T#${secondsSinceStart} - ${setTimeoutCount} - avg: ${setTimeoutTotalCount / secondsSinceStart}`);
lastTime = now;
setTimeoutCount = 0;
}
}, 1);
const nativeSetTimeout = self.setTimeout;
self.setTimeout = function() {
setTimeoutCount++;
setTimeoutTotalCount++;
return nativeSetTimeout.apply(this, arguments);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment