Skip to content

Instantly share code, notes, and snippets.

@patoi
Created June 14, 2019 16:11
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 patoi/76ec1bd6fb04a6b68ab3b7966c51e498 to your computer and use it in GitHub Desktop.
Save patoi/76ec1bd6fb04a6b68ab3b7966c51e498 to your computer and use it in GitHub Desktop.
'use strict'
// this is run only on v12, because private field
const ITERATION = 1000
console.log(
process.version,
ITERATION,
'iteration',
'with Type and freeze – v2: v12 only, private field'
)
/**
* Type enumeration.
* @readonly
* @enum {string}
*/
const Type = {
/** Homo sapiens sapiens */
HUMAN: 'HUMAN',
/** brrr... zz... */
ROBOT: 'ROBOT'
}
Object.freeze(Type)
// class test
class Human3 {
#iq
#name
#type
constructor ({ iq, name }) {
this.#iq = iq
this.#name = name
this.#type = [Type.HUMAN]
}
setIQLevel (iq) {
this.#iq = iq
}
getIQLevel () {
this.#iq
}
setName (name) {
this.#name = name
}
getName () {
return this.#name
}
getType () {
return this.#type
}
get () {
return {
type: this.#type,
iq: this.#iq,
name: this.#name
}
}
}
let i = 0
let KatrineBrewster
let start = process.hrtime.bigint()
while (i < ITERATION) {
KatrineBrewster = new Human3({ iq: 154, name: 'KatrineBrewster' })
i++
}
let end = process.hrtime.bigint()
let classExampleTime = end - start
console.info(`CLASS with private field: ${classExampleTime} nanosec.`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment