Created
June 14, 2019 16:11
-
-
Save patoi/76ec1bd6fb04a6b68ab3b7966c51e498 to your computer and use it in GitHub Desktop.
This file contains 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
'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