I am currently making a game with JavaScript, and I'm trying to make a for loop faster, as there are large randomly generated worlds, and I would prefer to make them onload, perhaps in a loading screen. A while back, I made this code:
colorMode(3);
background(255);
noStroke();
frameRate(0);
var x = 0;
var y = 0;
var offX = 0;
var offY = 0;
var chunkSize = 81;
var gen = 1;
var s = millis();
draw = function() {
while (x < offX + chunkSize) {
while (y < offY + chunkSize) {
fill((x + y) / 3, 255, 255);
rect(x, y, gen, gen);
y += gen;
}
y = offY;
x += gen;
}
offX += chunkSize - 1;
if (offX >= width) {
offY += chunkSize - 1;
offX = 0;
}
if (offY >= height) {
offY = 0;
offX = 0;
if (gen > 1) {
gen = round(gen - 1);
} else {
println("Finished. Took " + (millis() - s) + "ms");
noLoop();
}
}
x = offX;
y = offY;
};
on Khan Academy. It made it a lot faster than if I were just to do:
var x = 0;
var y = 0;
while (x < width) {
while (y < height) {
point(x, y);
++y;
}
++x;
}
Which, judging by this link, should be the fastest. Technically, it is, but it is laggy and somewhat slow. I want to replicate it, but I am having trouble. Possibly because this time it will be 3D. I could likely recreate it again in 2D, but I'm not sure. The newer code that I'm having trouble with is
let x, y, z;
let size = 10000;
let world = [];
while(x < size){
world[x] = [];
while(y < size){
world[x][y] = [];
while(z < size){
world[x][y][z] = Math.random();
++z;
}
++y;
}
++x;
}