Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
indexOf and includes in the background - JS

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.

EDIT:

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.`);
}

Why does this happen? enter image description here

Tested in google apps script

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda