Head of Product at Temporal. Previously lead architect and low-level systems programmer for scale out SaaS offering. Game engine developer, ML engineering expert. DMs open on Twitter.
A vanilla for loop is one of the least parallel constructs that exists in programming. At my last job, one of the teams I led spent months trying to come up with a strategy to convert traditional R lang for-loops into automagically parallel code. It's basically an impossible problem with modern technology. The difficulty arises because of a single pattern possible with for loops.
This code only produces the intended result if it is executed in order, iteration by iteration. If you tried to execute multiple iterations at once, the processor might incorrectly branch based on inaccurate values, which invalidates the result. We would be having a different conversation if this was C code, as the usage is different and there are quite a few tricks the compiler can do with loops. In JavaScript, traditional for loops should only be used if absolutely necessary. Otherwise utilize the following constructs:
map
// in decreasing relevancy :0consturls=['google.com','yahoo.com','aol.com','netscape'];constresultingPromises=urls.map((url)=>makHttpRequest(url));constresults=awaitPromise.all(resultingPromises);
for-each
consturls=['google.com','yahoo.com','aol.com','netscape'];// note this is non blockingurls.forEach(async(url)=>{try{awaitmakHttpRequest(url);}catch(err){console.log(`${err} bad practice`);}});
Head of Product at Temporal. Previously lead architect and low-level systems programmer for scale out SaaS offering. Game engine developer, ML engineering expert. DMs open on Twitter.
Probably the use case hasn't really emerged well. Maybe with an infinite stream of data or if you have to build a lazy parser for something like HTML or XML...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm actually writing a post which touches on this exactly. Figured I might as well post the relevant part of it here:
A vanilla for loop is one of the least parallel constructs that exists in programming. At my last job, one of the teams I led spent months trying to come up with a strategy to convert traditional
R
lang for-loops into automagically parallel code. It's basically an impossible problem with modern technology. The difficulty arises because of a single pattern possible with for loops.This code only produces the intended result if it is executed in order, iteration by iteration. If you tried to execute multiple iterations at once, the processor might incorrectly branch based on inaccurate values, which invalidates the result. We would be having a different conversation if this was C code, as the usage is different and there are quite a few tricks the compiler can do with loops. In JavaScript, traditional for loops should only be used if absolutely necessary. Otherwise utilize the following constructs:
map
for-each
Do you know JS has async iterators and generators?
Yes, and I do everything in my power to stay the hell away. Why would I want an
async
for loop? That's just a map with extra steps.Probably the use case hasn't really emerged well. Maybe with an infinite stream of data or if you have to build a lazy parser for something like HTML or XML...