Skip to content

Instantly share code, notes, and snippets.

@KoryNunn
Created February 21, 2014 06:43
Show Gist options
  • Select an option

  • Save KoryNunn/9129854 to your computer and use it in GitHub Desktop.

Select an option

Save KoryNunn/9129854 to your computer and use it in GitHub Desktop.
Counts the number of unique object instances within an object. It's slow.
function countObjChildren(thing){
var visited = [];
function count(thing){
for(var key in thing){
var prop = thing[key];
if(prop != null && typeof thing[key] === 'object'){
if(visited.indexOf(prop)<0){
visited.push(prop);
count(prop);
}
}
}
}
count(thing);
return visited.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment