So, lets say that we have an uint256[][] array. Using solidity 0.5.16.
uint256[][] example_array;
Every time a function is called, that array has an array of four elements assigned.
function n() public {
example_array.push([1, 2, 3, 4]);
}
function f() public {
for (uint index; index < example_array.length; index++) {
do stuff here
}
}
So after n interactions with the function, the array example_array will be enlarged, which can reach gigantic numbers, so the loop of the f() function will also be large.
However, let's say you have another function that finds out which arrays inside example_array are not needed and does the process of
delete example_array[m];
example_array.length--;
Would it be cheaper, in relation to the cost of gas, to use the function and delete the unnecessary elements (paying gas in the process) than simply ignoring the unnecessary elements (however the f() function would pay more gas)?