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

150
Vistas
Performance implications of return vs this in a javascript function

What is the difference between following code bits:

function Foo(context) {
  let mode;

  function setMode(m) {
     mode = m;
  }

  function doStuff() {
    if(mode === 'param') {
      // ... do stuff with context
    } else {
      // ... do different stuff with context
    }
  }

  this.setMode = setMode;
  this.doStuff = doStuff;
}

vs

function Bar(context) {
  let mode;

  function setMode(m) {
     mode = m;
  }

  function doStuff() {
    if(mode === 'param') {
      // ... do stuff with context
    } else {
      // ... do different stuff with context
    }
  }

  return {
    setMode,
    doStuff,
  }

}

In Foo I use this to 'augment' the new instance of Foo function:

const f = new Foo();
f.setMode('param');
f.doStuff(stuff);

In Bar I return 'new' object with methods each time function Bar is called:

const b = Bar();
b.setMode('parampam');
f.doStuff(stuff);

Are there any benefits (performance? memory usage?) of using one way or another? return vs this?

I am familiar with lexical 'this' scope, and how closure works but I'd like to know if above choice is purely syntactical or it has performance implications?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The performance implications will be trivial: don't worry about those unless this code is getting called a lot, and in that case it's better to benchmark both approaches to see how they're impacting performance in your specific case.

There are a few behavior differences involved. For example, new Foo().constructor points to the Foo function, whereas the object returned by calling Foo() or Bar() without the new keyword doesn't. Also, you can use the instanceof operator to check whether an object is an instance of Foo (meaning whether the object was constructed by calling new Foo()).

So you want to think about how you want users to think about your function.

If you want users to feel like they're creating an instance of a specific class type, and interacting with that class, use the first pattern. The fact that you have functions on that object that act like instance methods makes me think that's what you're going for in this case.

If you want users to feel like the function they're calling is going to procure an object and return it, use the second pattern. In that case, I'd probably rename Bar to something like getBar() or createBar().

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