const width=512
const height=512
img = ctx.createImageData(width,height)
//data = a 2D array that I'm trying to map to img
function data_to_rgb(h,w,data,img){
//modify the img object at h,w with data at h,w
//return nothing
}
function draw_loop(){
//some code that acts on data
for(let w=0;w<width;++w){
for(let h=0;h<height;++h){
data_to_rgb(w,h,data,img)
}
}
ctx.putImageData(img,0,0)
}
How do I convert this piece of code to start executing data_to_rgb in parallel, wait for all of them to finish and then execute ctx.putImageData(img,0,0)? I've looked into how to do this myself but so far all the examples showing how to do this keep using functions that has no arguments, where as mine got 4.
I tried the code below but the performance went down to about ~1/10th. I've spent too many hours trying to solve this on my own.
const width=512
const height=512
img = ctx.createImageData(width,height)
//data = a 2D array that I'm trying to map to img
async function data_to_rgb(h,w,data,img){ //MADE THIS ONE ASYNC
//modify the img object at h,w with data at h,w
//return nothing
}
async function draw_loop(){ //MADE THIS ONE ASYNC
//some code that acts on data
let tasks = [] //DEFINED THIS TASKS LIST
for(let w=0;w<width;++w){
for(let h=0;h<height;++h){
tasks.push(data_to_rgb(w,h,data,img)) //PUSHED IT INTO THE TASKS LIST
}
}
await Promise.all(tasks) //ADDED THIS AWAIT
ctx.putImageData(img,0,0)
}
Am I misusing async? (Maybe it should be... more used for network related things)
Is my attempt at solving it on my own even doing what I want it to do?
Here is the actual code
Here is the code with async in action
Here is the code without async in action