I have a nested for loop with
for(let i=0; i<500; i++){
//some work
for(let j=0; j<100000; j++){
//some work
}
}
is there any way I can make this parallel or fast like a web worker, asynchronous?
I could not think of how I can make any of these work.
Each work inside the inner for loop is independent of than watch others.
I tried to parallelize the code using the Promise.all() but no fruitful
for (let m = 0; m < keyCountMap.length; m += 2) {
fetchWorker.postMessage([nodePages, keyCountMap]);
// console.log(m, keyCountMap[m], keyCountMap.length);
let myRoot = nodePages[keyCountMap[m]];
const view = await Copc.loadPointDataView(filename, copc, myRoot);
let getters = ["X", "Y", "Z", "Intensity"].map(view.getter);
let chunkCount = 2;
let totalCalled = 0;
for (let j = 0; j < keyCountMap[m + 1]; j += chunkCount) {
let remaining = keyCountMap[m + 1] - totalCalled;
let localChunkCount = Math.min(chunkCount, remaining);
totalCalled += localChunkCount;
const pointTemp = new Array(localChunkCount).fill(null);
const promises = pointTemp.map((element, index) =>
readPoints(index + j, getters)
);
const done = await Promise.all(promises);
}
}
and
const readPoints = (id, getters) => {
return new Promise((resolve, reject) => {
let returnPoint = getXyzi(id, getters);
positions.push(
returnPoint[0] - x_min - 0.5 * width,
returnPoint[1] - y_min - 0.5 * width,
returnPoint[2] - z_min - 0.5 * width
);
const vx = (returnPoint[3] / 65535) * 255;
color.setRGB(vx, vx, vx);
colors.push(color.r, color.g, color.b);
return resolve(true);
});
};
function getXyzi(index, getters) {
return getters.map((get) => get(index));
}