Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Should I use a for loop to iterate over my object's array?

I have created a DomObject class that can display boxes that move around.

jFiddle

Naturally, if I increase the number of boxes in the DomObject, the movement function takes longer to run since it is a for loop.

 beginMovement = () => {
    if (!this.timer) {
      // console.log("Begin Movement");
      this.timer = setInterval(this.movement, this.time);
    }
  };
 movement = () => {
    // here we know that its running
    let i = 0;
    while (i < this.boxes.length) {
      this.boxes[i].move();
      i++;
    }
  }

In the case that I increase the length of this.boxes, I notice that there are performance issues.

So my main question is, should I be using a for loop in this instance? Or should I avoid using basic html for moving items all together and move onto using canvas

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Depends what your goal is. You seem to be trying to do some sort of an animation in which case using canvas/WebGL is a better option if you are going for raw speed.

Now your objects are living on the DOM, which wasn't originally designed to display fancy graphics and animations. Every design consideration for canvas was made explicitly for animation and complex bitmap operations like colorization and blurring. You never have to "find" a canvas element. You can access them far more quickly than even a cached DOM element. Even updating text elements in canvas is faster then doing so on the DOM.

Here is a related SO discussion on canvas vs DOM.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda