While this could also be considered a general JS question more suited to StackOverflow, I decided since it relies on functions available exclusively in Roll20 I should try here first. I have a code snippet that I'm trying to optimize. The goal is to grab all tokens that meet the following critiera: The token must be controlled by the player submitting the command OR the player submitting the command must be the GM. The token must be on the current page. The token must represent a character (no drawings). The token must be on the same layer that the player submitting the command is viewing. Not quite sure how to implement this. If it's not possible, then just limit it to the token layer for non-GMs and the token & GM layers for the GM. For most players, this will return 1 token, or a small number of tokens for players using characters with "pets". However, when used by the GM, this should return all NPC and PC tokens on the layer(s). I think my statements above restricts the list appropriately. My current code is: var allPlayerTokensOnPage = _.filter(findObjs({ type: 'graphic', _pageid: Campaign().get('playerpageid') }),function(filterItem){
return (
(_.contains(filterItem.get('controlledby'),command.playerid) && filterItem.get('layer') == 'objects')
|| (playerIsGM(command.playerid) && _.contains(['objects','gmlayer'],filterItem.get('layer')))
) && filterItem.get('represents');
}); I'm not sure how well JS handles || operators when it comes to optimization. I know it's a nightmare in most DBMSs, and suboptimal in C#. The idea here is that if I have a page with a lot of graphics on it, I want to iterate through the list as few times as possible, and keep as many things out of the list as possible before I iterate through the list.