DEV Community

Discussion on: Emulating "Private" Variables in JavaScript with Closures and Factory Functions

Collapse
 
qm3ster profile image
Mihail Malo

I wonder what the memory performance is like compared to this:

const privates = new WeakMap()
class Animal {
  constructor(name, job) {
    privates.set(this, { name, job })
    this.aPublicOne = true
  }

  // Real Getters
  get name() {
    return privates.get(this).name
  }
  get job() {
    return privates.get(this).job
  }
  // Real Setters
  set name(name) {
    privates.get(this).name = name
  }
  set job(job) {
    privates.get(this).job = job
  }
}
Enter fullscreen mode Exit fullscreen mode

Liek, I know instantiating closures isn't the same as actually declaring separate different functions. But it also is more expensive than object methods, to my great disdain.
Will this be somewhere in the middle, or actually worse than both?

Collapse
 
somedood profile image
Basti Ortiz

I'm not sure myself, honestly. I'd love to know. When I have the time, I'll try to create a rigorous experiment for this using the DevTools Memory tools.

If I were to make an educated guess, I'd say the difference in memory efficiency would be negligible.

Collapse
 
qm3ster profile image
Mihail Malo

How funny is it, though, that this method is still using closures, the 5 functions capturing privates :D

Thread Thread
 
somedood profile image
Basti Ortiz • Edited

It makes you realize that even though closures are so ubiquitous in JavaScript, not everyone truly understands it.

Collapse
 
denik1981 profile image
denik1981
const privates = new WeakMap()
class Animal {
  constructor(name, job) {
    privates.set(this, { name, job })
    console.log('I'm a total GENIUS!')
  }
}
// A few lines later on the code ....
function privates() {
  console.log("No, you are not. Do you remember the NO-GLOBALS rules?")
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
qm3ster profile image
Mihail Malo

1. Uncaught SyntaxError: missing ) after argument list

You would have caught this one if you used highlighting:

const privates = new WeakMap()
class Animal {
  constructor(name, job) {
    privates.set(this, { name, job })
    console.log('I'm a total GENIUS!')
  }
}
// A few lines later on the code ....
function privates() {
  console.log("No, you are not. Do you remember the NO-GLOBALS rules?")
}
Enter fullscreen mode Exit fullscreen mode

vs

const privates = new WeakMap()
class Animal {
  constructor(name, job) {
    privates.set(this, { name, job })
    console.log("I'm a total GENIUS!")
  }
}
// A few lines later on the code ....
function privates() {
  console.log("No, you are not. Do you remember the NO-GLOBALS rules?")
}
Enter fullscreen mode Exit fullscreen mode

2. Uncaught SyntaxError: Identifier 'privates' has already been declared (pointing at the function, not the const)

Have you forgotten declaration collision? (TypeScript would also have caught it statically)

3. "globals" are fine when you are thinking in modules