So I have scripts I've written for a turn based (originally miniatures based) wargame we're playing on Roll20. While things work, I am trying to optimize for speed. Background: Tokens on the map represent individual squads of infantry or individual armour units. 'Character Sheets' are basically the unit information with things like armour, weapons information etc. Terrain on the map is 'outlined' by lines that are used (like lines in the DLL) to define the terrain. I use ray-tracing math (intersection of lines between shooter and target and terrain lines) or 'point in polygon' math to determine what terrain a target is in (for cover purposes). This generally means I am doing multiple findObjs for the tokens attacking/being attacked (multiple, as the game is unit based - so a formation of say 7 tanks fires on another formation of 4 tanks for instance) and then getObjs to get information on the various units abilities. I do store the terrain line information in the state variable, as it doesn't change during the game, as well as using state to store certain other information such as the list of tokenIDs in a given formation. I have optimized my LOS and terrain functions - rather than iterating through all terrain I use bounding boxes to only iterate through terrain in the area the LOS line 'crosses' or that a token could be in then running the polygon math to check if it really is. Typically there are 150-200 tokens on a map to start, representing perhaps 50 unit types (so 50 characters). Problem: However, I still find some slowness in the API, almost as if it hesitates when first starting up then running the code. Before I streamlined some of the code, I could get an 'infinite loop' error in large pitched battles, due to timeout rather than an actual infinite loop. So my questions for those with more knowledge than I: - is there a way to find out how long a given routine or section of code takes to run? - I have thought of doing all the initial findobjs/getobjs when the game is 'setup' (before any turns are done), and storing all relevant info in state - but am not sure if that really speeds things up - ie. the lookup from an array in state vs. a findObjs or getObjs - as I am not sure how long the relevant lookups take - this last would also require me using an on change event each time a token is moved to update the X,Y coordinates in the state array - however I figure that doing this wouldnt be noticeable given only one token is moved at a time - alternately I could do a mix - store character(unit) information in state and then just use findObjs when I need to do LOS/distance calculations on tokens Thoughts and comments welcome! Don