Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the fastest way to loop through an array of 121 items?
I was wondering because (skip to what you have tried)

What I have tried:

I have tried looping using forEach() function and it causes my game to slow down to 10-25 fps
so that's why I want some recommendations for fast looping
Updated 11-Dec-22 7:54am
PIEBALDconsult 19-Jun-21 11:38am    
I don't know about Javascript, but in C#, it's better (when possible) to use a simple for (or while) rather than a foreach -- and an array definitely doesn't require foreach.
Additionally, with a large enough (sorted) array or list, a binary search may be best.

A "foreach" is slower than a stright up "for" indexer because the foreach has to maintain an enumerator object over the collection. Calling the navigation functions of the collection imparts an overhead multiplied by the number of items in the collection.

A plain "for" loop is just an indexer into an array of values/objects. Basically, a for loop just increments a pointer into an array and has very little overhead.

In order to make the for loop faster, you'd have to have multiple threads, each working on a smaller segment of the entire array, but setting up and managing the threads itself may add more overhead than its worth.

As what's already been pointed out, it's not the speed of iterating over the array that's the problem. It's more going to be what you're doing to each element in the array that will slow things down.
Share this answer
One way (but tricky) is using parallel functions, see: How to run async JavaScript functions in sequence or parallel[^]
This way you could use several functions that only process a 'chunk' of the array.

Also see: which-type-of-loop-is-fastest-in-javascript-ec834a0f21b9[^]
Share this answer
Coder2195Text 11-Feb-21 9:38am    
Is that the fastest way
RickZeeland 11-Feb-21 9:39am    
Hard to say, guess it depends on the situation and how your code looks, let's wait and see if someone comes up with anything better :)
What is the fastest way to loop thorugh a long array

First of all, a list of 121 is not long, long usually start after 100,000.
Whatever the flavor of the loop (for, while ...), by itself, it is fast.
What make a loop slow is what you do inside the loop.

The problem is that you told nothing of interest, we have no idea of what is the game or how you did the loop.

Before thinking to multithreading, you need to think about optimization.
Program optimization - Wikipedia[^]
Share this answer
Consider the response, above, that ultimately references you towards code optimization.

There are a few things you can do (no code, so these are rather general) to speed up processing.

0 - Do any calculations and value setting that are not modified within the loop outside of the loop. No need to repeatedly set a constant to the same value.
1 - if you're calculation values from a limited set (such as powers of 2, or something like that, use a lookup table instead of the calculations. If you can just use an index into a table of pre-calculated values you can speed things up by saving, potentially, an immense number of calculations. See, also, item 0
2 - if your loop has internal nested loops, see if you can "unroll them" into straight series of calculations. This is most feasible if you have a fixed number of elements for any the inner loops.
3 - If you have internal functions, if they're very simple then just plug in the code, instead. Calling a function and returning is a lot of overhead.

These are just a few and they don't all apply all the time. Use good judgement.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900