Skip to content

Instantly share code, notes, and snippets.

@Ultrabenosaurus
Created October 16, 2012 08:52
Show Gist options
  • Save Ultrabenosaurus/3898131 to your computer and use it in GitHub Desktop.
Save Ultrabenosaurus/3898131 to your computer and use it in GitHub Desktop.
get the top-level size of a JavaScript object in all browsers
// credit to Sam (https://github.com/sambenne) for this
// saving as Gist so I don't lose it.
function objectLength(obj) {
try {
return Object.keys(obj).length;
} catch(err) {
var total = 0;
for(var k in obj) {
if(obj.hasOwnProperty(k)) {
total++;
}
}
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment