Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Created March 28, 2013 15:11
Show Gist options
  • Save AutoSponge/5263915 to your computer and use it in GitHub Desktop.
Save AutoSponge/5263915 to your computer and use it in GitHub Desktop.
(function (global, known) {
global.dev = global.dev || {};
function register(str) {
known[str] = str;
}
function raiseError(leaks) {
if (leaks && leaks.length) {
throw new Error("global leak" +
(leaks.length > 1 ? "s" : "") +
" detected: " + leaks.join(", "));
}
}
function fromKnown(key) {
return key !== known[key];
}
global.dev.checkGlobals = function (debug) {
var obj = {}, type, val, leaks = Object.keys(global).filter(fromKnown);
if (debug) {
leaks.forEach(function (leak) {
val = global[leak];
type = typeof val;
obj[type] = obj[type] || {};
obj[type][leak] = val;
});
console.log("global leak detected:");
console.dir(obj);
} else {
raiseError(leaks);
}
};
global.dev.registerAllGlobals = function () {
Object.keys(global).forEach(register);
};
global.dev.registerGlobal = register;
}(this, {}));
if (Object.keys && Array.prototype.forEach) {
dev.registerAllGlobals();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment