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 : '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, result) {
for(var key in o) {
if(o.hasOwnProperty(key)) {
result.progress += 1;
var current = o[key];
if(typeof current === 'object') {
if(current.constructor == Array) {
output.trace(result.indent + 'A: ' + key + ' (length: ' + current.length + ')');
result.indent += ' ';
for(var i = 0, n = current.length; i < n; i ++) {
output.trace(result.indent + 'item ' + i + ':');
footprint_for(current[i], result);
}
result.indent = result.indent.substring(0, result.indent.length - 2);
}
else {
output.trace(result.indent + 'O: ' + key);
result.indent += ' ';
footprint_for(current, result);
result.indent = result.indent.substring(0, result.indent.length - 2);
}
}
else if(typeof current === 'function') {
output.trace(result.indent + 'F: ' + key);
output.debug(current.toString(), result.indent);
result.indent += ' ';
footprint_for(current.prototype, result);
result.indent = result.indent.substring(0, result.indent.length - 2);
}
}
}
}
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 result = { progress: 0, indent: ' '};
footprint_for(o, result);
overall += result.progress;
output.print(key + ' subtotal: ' + result.progress, 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