It depends on the size of the array, how deeply buried the first match is, and the complexity of the test that you're performing on each element. But using Chrome, with a small array like the OP is using (only 7 elements),
.some
is actually much slower than a for loop.
For example, take this code:
JavaScript:
(() => {
const arr = [21, 53, 10, 19, 4, 7, 32];
var result = false;
console.time("For");
for (let i = 0; i < arr.length; i++) {
if (arr[i] == 7) {
result = true;
break;
}
}
console.timeEnd("For");
console.log(result);
})();
vs this code:
JavaScript:
(() => {
const arr = [21, 53, 10, 19, 4, 7, 32];
console.time("Some");
var result = arr.some(element => element == 7);
console.timeEnd("Some");
console.log(result);
})();
The first snippet uses a for loop to search a 7-element array for a value that is located 6 elements deep, and the second is a
.some
implementation that does the same thing. After testing each snippet 5 times on Chrome, and averaging the results, the .some snippet turned out to be 3.6x slower on my system.
However, when I performed the same test on a very large array (
[...Array(50000).keys()]
), things were a little different. The for loop was the still the winner up until the point where the first match was buried about 2000 elements deep. At that point, both implementations were tied. After that point,
.some
became increasingly superior. When there was no match, and the entire 50k elements had to be looped through, the for loop implementation was about 1.6x slower.
If I were to venture a guess, I'd say that under the hood,
.some
is probably using a multi-threaded approach to search the array. For small arrays, the extra overhead required to set that up outweighs the benefits. But for very large arrays, it is superior. But obviously, results will vary on different systems and in different browsers.
TLDR: A for loop is actually much faster in this situation, and probably most things that you'd be doing in a RPG Maker plugin.
.some
is faster with very large data sets, and probably scales better with better hardware.