I could use some help figuring out how I can speed up the execution of my code, or at least stop the system from thinking my code is in an infinite loop. I wrote the DupCharToken API, which will duplicate a character and a token multiple time. This is handy for GMs that don't like Mooks, they can just click a token and tell it duplicate the character sheet and token X number of times, giving lots of copies of the token, each of which has its very own character sheet. The problem is that if you try to make too many of them, you get the " Possible infinite loop detected, shutting down. " error, and the sandbox needs to be restarted. When I first made the API, I tested it and it would copy up to 40 simple characters before the error would appear. However I am getting complaints that it is happening with fewer and fewer of number of duplicates requested. Today I tried to duplicate a PC in a large campaign that had hundreds of characters (including npc's and monsters), and it would not make even one copy of a PC. I traced it down, and it depends upon the number of attributes (including repeating section attributes) the character has. Specifically, each instance of createObj('attribute', sa); Takes 35 or more milliseconds to run. Thus copying/creating 100 attributes is going to take more than 3.5 seconds to run. And creating more than 1000 attributes is going to cause it to time out. This might be 5 characters that have 200 attributes each, or 1 character with more than 1000. Use of repeatingt sections means that even simple monsters usually have at least 200 attributes. An interesting thing is that running BOTH let newCObj = createObj('character', newC); and let newTObj = createObj('graphic', newT); completes in under 8 ms. So creating attributes seems to be taking more than 9 times longer than creating ether tokens or characters and I am not certain why. Anyway, I am hoping that somebody can explain why create attribute is taking so much longer, and suggest a way I can work around this. Basically in my code I get the token and character, and create duplicates. Then I get all the attributes and copy them using... _.each(findObjs({_type:'attribute', _characterid:oldCid}),(a)=>{ // Copy each attribute
let sa = JSON.parse(JSON.stringify(a));
delete sa._id;
delete sa._type;
for( let i = 0; i < charArray.length; ++i ) {
delete sa._characterid;
sa._characterid = charArray[ i ].id;
if( links.indexOf( sa.name ) > -1 ) {
let newA = createObj('attribute', sa);
tokenArray[ i ].set( "bar" + (links.indexOf( sa.name ) + 1).toString() + "_link", newA.id); // Link the new token to the new attribute.
} else
createObj('attribute', sa);
};
});
Any ideas?