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

83
Vistas
How to convert a nested for of loop into a custom iterator?

I have the following code:

function* getPossibleCollidersWith(shape) {
  for (let square of retrieve(shape)) {
    for (let neighbor of square) {
      yield neighbor;
    }
  }
}

This works, but uses generators and yield, which is very slow. I've benchmarked it using Chrome's profiler and it is significantly less performant than removing the generator and just iterating through the nested loop in the client code.

However, it's a lot of boilerplate to do a nested loop every time I want to get a shape's neighbors. I would like to create a custom iterator to see if that has better performance than the generator function, but all the examples I see are rather complex and involve keeping track of indicies, null values, etc.

Is there a simple way to convert the above code into a custom iterator? Ideally I'd like to keep using for of loops to return each element, but all the examples I see online involve error-prone index-tracking.

I would like the client code to look like this, if possible:

for (let neighbor of getPossibleCollidersWith(shape)) {
  // handle neighbor
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function* getPossibleCollidersWith(shape) {
  for (let square of retrieve(shape)) {
    yield* square;
  }
}
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