Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda