There is a common mistake that a lot of coders do, which is to check the length property in the loop body several times. When you make a for loop:
JavaScript:
for (let i = 0; i < $gameMap.events().length; i++)
Each and every time he runs, it will take a property lookup for $gameMap.events().length.
We do not need this(unless the length of the iterable array or object can change during the loop).
And also it will be searching for a 'variable' outside the loop scope in each loop.
Instead of that, we can use a local variable to save time and just do only property lookup:
JavaScript:
for (let i = 0, len = $gameMap.events().length; i < len; i++)
There is also another thing to speed up the loop if the order to iterate the array doesn't matter.
Is reversing the order in which the loop runs, like this:
JavaScript:
for (let i = $gameMap.events().length;i--;)
We see now that we just set the length property to the iterator variable.
And we set the loop control condition to
'i--'.
Control conditions are compared to the true value. And any number that is not zero, is already true.
So now, we are making a simple comparison against 0, which means that before reversing the order we are making two comparisons inside the control condition, per iteration:
'i < len = Is iterator value less than the total(len) and this value is true?'
But after reverse the order, we are only making one comparison per iteration:
'i-- = Is this value true?'
Example in line 521:
JavaScript:
for(let i = $gameMap.events().length; i--;){
if ($gameMap.events()[i]) {
for (let j = lightarray_id.length; j--;) {
if (lightarray_id[j] == lightid) {
let mapid = $gameMap.mapId();
let eventid = $gameMap.events()[i]._eventId;
let key = [mapid, eventid, 'D'];
$gameSelfSwitches.setValue(key, false);
}
}
}
}