Skip to content

Instantly share code, notes, and snippets.

@coreyti
Created June 24, 2009 16:27
Show Gist options
  • Save coreyti/135367 to your computer and use it in GitHub Desktop.
Save coreyti/135367 to your computer and use it in GitHub Desktop.
// (maybe) a way to approximate the current "footprint" of
// your javascript (in a firebug-enabled firefox).
//
// example:
// footprint({
// context : jQuery,
// initial : true,
// print : true,
// trace : true,
// debug : true,
// filter : function(o) { return true; }
// });
function footprint(options) {
var options = $.extend({
context : window.location, // the top-level entry point. e.g., context: jQuery
initial : false, // flag: include items loaded before this file
print : false, // flag: print names and totals.
trace : false, // flag: print keys.
debug : false, // flag: print function bodies.
filter : false // used to limit first-level entry. e.g., filter: function(o) { return o.foo !== undefined; }
}, options);
var overall = 0;
var context = options.context;
var output = {
print : function(message, tail) {
if(options.print) {
console.log(message);
if(tail) {
console.log(' ');
}
}
},
trace : function(message) {
if(options.trace) {
console.log(message);
}
},
debug : function(message, indent) {
if(options.debug) {
console.debug(message.replace(/^/gm, indent + ' > ').replace(/^(.{1,150}).*/gm, '$1'));
}
},
};
function footprint_for(o, progress, indent) {
if(undefined === progress) {
progress = 0;
indent = ' ';
}
for(var key in o) {
if(o.hasOwnProperty(key)) {
progress += 1;
output.trace(indent + key);
if(typeof o[key] === 'object') {
indent += ' ';
progress += footprint_for(o[key], progress, indent);
indent = indent.substring(0, indent.length - 2);
}
if(typeof o[key] === 'function') {
output.debug(o[key].toString(), indent);
}
}
}
return progress;
}
for(var key in window[context]) {
if(! options.initial) {
if(window.__footprint_initials__[context] && window.__footprint_initials__[context][key]) {
continue;
}
}
var o = window[context][key];
if(o) {
if( ! options.filter || options.filter(o)) {
output.print(key);
var subtotal = footprint_for(o);
overall += subtotal;
output.print(key + ' subtotal: ' + subtotal, true);
}
}
}
output.print('total: ' + overall);
return overall;
}
__footprint_initials__ = {};
for(var global in window) {
__footprint_initials__[global] = {};
try {
for(var top in window[global]) {
__footprint_initials__[global][top] = true;
}
}
catch(e) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment