I do wonder how indexOf and includes work in the background.
They work like loops which checks the whole array if an element is in what index or if it exists. Are they actually looping in the background?
Also, if someone could provide resources about topics like this, that would be great. I tried searching in google and existing posts here but to no avail. My search terms might not be good.
I am trying to do a comparison test and almost always, includes is faster when identifying if a blank cell is present.
The issue is this difference in time that does show negative number randomly.
function myFunction() {
array = [1, 2, 3, "", 5];
var t0 = new Date().getMilliseconds();
j=0
while(j<10000000) {
x = array.indexOf("") > -1;
j++;
}
var t1 = new Date().getMilliseconds();
console.log(`Call to indexOf took ${t1 - t0} milliseconds.`);
i=0;
var t2 = new Date().getMilliseconds();
while(i<10000000) {
y = array.includes("")
i++;
}
var t3 = new Date().getMilliseconds();
console.log(`Call to includes took ${t3 - t2} milliseconds.`);
}
Tested in google apps script
getMilliseconds is the milliseconds at the current time. It is not the total milliseconds.
Time 1: 1 minute 300 milliseconds
Time 2: 2 minutes 100 milliseconds
100 - 300 = -200
You want to use something like performance.now
const t1 = performance.now();
// some process
const t2 = performance.now();
console.log(t2-t1);
or just subtracting two dates
const t1 = Date.now()
// some process
const t2 = Date.now();
console.log(t2-t1);