Optimizing script to save CPU time

  • First of all, thank you for reading this post 🙂

    Right now I have a script, which iterates through my rooms and determins every tick whether or not a new screep needs to be send to the spawnqueue. For this i use the following filters:

    Creeps that operate within the room: var workers = room.find(FIND_MY_CREEPS, { filter: (creep) => { return (creep.memory.role == role) && (creep.room == room) }});

    Creeps that operate outside the room: var workers = _.filter(Game.creeps, (creep) => { return (creep.memory.role == role) && (creep.memory.homebase == room.name) && (creep.memory.surroundingroom == 'none')});

    Creeps that operate in the direct surrounding rooms: var workers = _.filter(Game.creeps, (creep) => { return (creep.memory.role == role) && (creep.memory.surroundingroom == surroundingroom)});

    Especially the last two scripts use a lot of CPU time, since for every room/surroundingroom the script searches through all of my creeps.

    Now I think I can save CPU if I filter my creeps once and giving this object as an input variable for each room Within each room I then can filter roles and other things and putting that result als input variable for different functions

    I just want to check if my thinking is right, as this is a pretty big change....

    Is this the right way, or should I look somewhere else?