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

192
Visualizações
Have lots of immutable objects or a few mutable ones?

I have a Point class that represents x and y coordinates. One approach is to make the properties of that class immutable and constantly create new objects when needed, like so:

class Point {
    readonly x: number;
    readonly y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    add(x: number, y: number) {
        return new Point(this.x + x, this.y + y);
    }
}

document.addEventListener("mousemove", (e) => {
    let p = new Point(e.clientX, e.clientY);
    let p2 = p.add(24, 42);
    // ...
});

Another approach is to create objects beforehand and reuse them, for example:

class Point {
    x: number;
    y: number;

    constructor(x?: number, y?: number) {
        this.set(x, y);
    }

    set(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    add(x: number, y: number) {
        this.set(this.x + x, this.y + y);
    }

    copy(p: Point) {
        this.set(p.x, p.y)
        return this;
    }
}

let p = new Point();
let p2 = new Point();

document.addEventListener("mousemove", (e) => {
    p.set(e.clientX, e.clientY);
    p2.copy(p).add(24, 42)
    // ...
});

My theory is that since JavaScript is garbage collected, following the first pattern and spewing objects shouldn't be a problem, as long as you don't leave references to them. However, I looked at how the popular Three.js library implements its Vector2 object which does basically the same as what I've shown above, and it follows the second pattern.

The first approach seems more maintainable and easy to use, but the second one is maybe more memory-efficient?

When talking about design patterns in general, it largely comes down to preference and opinion. My question is, in the context of JavaScript, is there a concrete reason to pick one or the other, or the performance differences are negligible?

about 4 years ago · Juan Pablo Isaza
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