Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
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 answers
Answer question

0

function* getPossibleCollidersWith(shape) {
  for (let square of retrieve(shape)) {
    yield* square;
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!