DEV Community

Discussion on: Optimized way to iterate over the length in js loop?

Collapse
 
nerdlyist profile image
Coury Ryan Richards

The next level question "is how can I figure out what is more optimal". Learning to profile your code is a huge benefit. Also, with questions like this you need to know what you are optimizing for memory, cpu, network speed, etc. If the ... is n API calls you have other concerns then if it is say file writes.

You should look into forEach, map or reduce. All of which solve specific cases for array iteration.

In the end this would be considered a micro optimization. Just get the coding done and refactor. Variable set vs function calls like size should not matter too much.

Collapse
 
warriorofsunlight profile image
WarriorOfSunlight

On the topic of array iteration and array functions, I would highly recommend that you take a minute to really look through the array prototype methods: developer.mozilla.org/en-US/docs/W...

There are several you may never end up using but having high level knowledge of the common ones (namely, forEach, map, filter, and reduce) is always useful. As Ryan has stated, the optimizations you're looking to make will save you a minimal amount of fetch/processing time at best.