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

143
Views
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 answers
Answer question

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 Report
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!