Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Is there a way to segment for loops so they're more efficient?

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;
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!