lil late, but its a good question, that I myself had..so for those looking, another option to countBy
I tend to use many variations of .sum like:
var x = _.sum(Game.creeps, (c) => c.memory.role == 'builder');
or other combos like
var x = _.sum(Game.creeps, (c) => c.memory.role == 'heavyworker' && c.memory.workingRoom == "E58N24" && c.memory.position == 1&& c.memory.upgradeMe==false && c.memory.recycleMe==false);
...or whatever else you might have defined in the memory