Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda