Created
August 11, 2020 12:47
-
-
Save addaleax/b7451a4f6cda596548c3e9eb32a8f92e to your computer and use it in GitHub Desktop.
Private vs Symbol vs Plain property comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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