Skip to content

Instantly share code, notes, and snippets.

@addaleax
Created August 11, 2020 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addaleax/b7451a4f6cda596548c3e9eb32a8f92e to your computer and use it in GitHub Desktop.
Save addaleax/b7451a4f6cda596548c3e9eb32a8f92e to your computer and use it in GitHub Desktop.
Private vs Symbol vs Plain property comparison
function bench(name, fn) {
const a = process.hrtime.bigint();
for (let i = 0; i < 1e7; i++)
fn();
const seconds = Number(process.hrtime.bigint() - a) / 1e9;
console.log({ name, seconds });
}
class Private {
#privateProp = 0;
}
const kSym = Symbol('kSym');
class WithSymbol {
constructor() {
this[kSym] = 0;
}
}
class Plain {
constructor() {
this.prop = 0;
}
}
bench('private', () => new Private());
bench('symbol', () => new WithSymbol());
bench('plain', () => new Plain());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment