I have written a script to make a roll for a skill for each selected token and send the output to the chat. If someone could help me how to speed up this script I would be very happy. In my campaign this takes about 5-15 seconds for 2 rolls - which means 2 selected tokens. I can wait a bit, but the script becomes so slow it does not feel like it is responding for the user anymore, altough it will finish when the user waits. However, the time between clicking the button and until the result shows simply became too long. This obviously is not the whole code and I tried to strip everything not directly related to what I consider responsible for the slowness. I guess the script is slow either because of the line var tokens = findObj /.... or _.each /.... I have no idea how performant _.each is ... or simply because of the slowness of rolling from the API with sendChat. However, is there any way to obviously increase the speed by for example doing all rolls in a single sendChat instead of issuing several sendChat commands? I am not really sure where to start to find out how to improve this script. var handleSkillCheck = function(msg,skill){
for(var s in msg['selected']){//FOR EACH SELECTED GRAPHIC
s = msg['selected'][s];
var id = s["_id"];
var tokens = findObjs({_type: 'graphic', _id: id,_subtype:"token"},{caseInsensitive: true});
for(var t in tokens){//FOR EACH SELECTED TOKEN
t = tokens[t];
var represents = t.get("represents");
var character = ...//get the character "represents"
var nameRegex = /repeating_skillsaction_[^_]+_skillsaction_id/; //each repeating row has an attribute id which I store using Sheet Workers such that I can retrieve a specific row
//from the API and Sheet Workers
var skillIDs = filterObjs(function(o){return o.get('type')==='attribute' && o.get('characterid') === represents && o.get('name').match(nameRegex);});
_.each(skillIDs,function(sid){
/* get the value of the repeating_skillsaction_<sid>_skillsaction_<field> from "character".
* This essentiallyare the fields of a row. Each of those is a findObj call looking for matching name and character id.
*/
var sname,satt,sbase,smod,....
if(sname.toLowerCase().trim() == skill.toLowerCase().trim()){
//If the name of the skill matches the name specified by "skill" then we want to rol
var pool = satt + sbase + smod;
var process = function(x) {
var RollValue = x[0].inlinerolls[0];
var roll = buildInline(RollValue, undefined, undefined); // this is stolen from powercards and does the html formatting for the inline rolls
var s = parseInt(RollValue["results"]["total"]);
var result = "&{template:roll}"...//format the output in a rolltemplate
//send "result" to player/gm
};
//roll from the API
sendChat(t.get("name"), "/roll [["+pool+"d6sacs5cs6>5]]", process);
}
});
}
}
};