Created
October 16, 2012 08:52
-
-
Save Ultrabenosaurus/3898131 to your computer and use it in GitHub Desktop.
get the top-level size of a JavaScript object in all browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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