Suppose you had an array like:
var arr= [{id:121, v:'a'}, {id:232, 'b'}];
And you needed to find id: 232 and remove it, so you might do:
for (var i = arr.length; i--;) {
if (arr[i].id === 232) {
arr.splice(i, 1);
}
};
And suppose there is an event handler which was adding items into the array like:
arr.push( {id:443, 'c'} );
Is it possible that the event handler might be called as the for loop is iterating? If so, then splice(i,1) would remove the wrong array index.
As javascript is single threaded, is it smart enough to finish the for-loop before handling events?