SHARE
TWEET

Untitled

a guest Jun 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const ITERATION = 1000
  4. console.log(
  5.   process.version,
  6.   ITERATION,
  7.   'iteration',
  8.   'with Type and freeze – v2: prototype based constructor function'
  9. )
  10.  
  11. /**
  12.  * Type enumeration.
  13.  * @readonly
  14.  * @enum {string}
  15.  */
  16. const Type = {
  17.   /** Homo sapiens sapiens */
  18.   HUMAN: 'HUMAN',
  19.   /** brrr... zz... */
  20.   ROBOT: 'ROBOT'
  21. }
  22. Object.freeze(Type)
  23.  
  24. // closure function test
  25.  
  26. const Human = () => {
  27.   const _type = [Type.HUMAN]
  28.   let _name
  29.   let _iq
  30.   return {
  31.     setIQLevel (iq) {
  32.       _iq = iq
  33.       return this
  34.     },
  35.     getIQLevel () {
  36.       return _iq
  37.     },
  38.     setName (name) {
  39.       _name = name
  40.       return this
  41.     },
  42.     getName () {
  43.       return _name
  44.     },
  45.     getType () {
  46.       return _type
  47.     },
  48.     get () {
  49.       return {
  50.         type: _type,
  51.         iq: this.getIQLevel(),
  52.         name: this.getName()
  53.       }
  54.     }
  55.   }
  56. }
  57.  
  58. let i = 0
  59. let SarahConnor
  60. let start = process.hrtime.bigint()
  61. while (i < ITERATION) {
  62.   SarahConnor = Human()
  63.     .setName('Sarah Connor')
  64.     .setIQLevel(154)
  65.   i++
  66. }
  67. let end = process.hrtime.bigint()
  68. let closureExampleTime = end - start
  69.  
  70. // console.log('Sarah Connor:', SarahConnor.get())
  71.  
  72. // constructor function test
  73.  
  74. function Human2 ({ iq, name }) {
  75.   this.iq = iq
  76.   this.name = name
  77.   this.type = [Type.HUMAN]
  78. }
  79. Human2.prototype.setIQLevel = function (iq) {
  80.   this.iq = iq
  81. }
  82. Human2.prototype.getIQLevel = function () {
  83.   this.iq
  84. }
  85. Human2.prototype.setName = function (name) {
  86.   this.name = name
  87. }
  88. Human2.prototype.getName = function () {
  89.   return this.name
  90. }
  91. Human2.prototype.getType = function () {
  92.   return this.type
  93. }
  94. Human2.prototype.get = function () {
  95.   return {
  96.     type: this.type,
  97.     iq: this.iq,
  98.     name: this.name
  99.   }
  100. }
  101.  
  102. i = 0
  103. let JohnConnor
  104. start = process.hrtime.bigint()
  105. while (i < ITERATION) {
  106.   JohnConnor = new Human2({ iq: 154, name: 'John Connor' })
  107.   i++
  108. }
  109. end = process.hrtime.bigint()
  110. let funcExampleTime = end - start
  111. // console.log('John Connor:', JohnConnor.get())
  112.  
  113. // class test
  114.  
  115. class Human3 {
  116.   constructor ({ iq, name }) {
  117.     this.iq = iq
  118.     this.name = name
  119.     this.type = [Type.HUMAN]
  120.   }
  121.  
  122.   setIQLevel (iq) {
  123.     this.iq = iq
  124.   }
  125.   getIQLevel () {
  126.     this.iq
  127.   }
  128.   setName (name) {
  129.     this.name = name
  130.   }
  131.   getName () {
  132.     return this.name
  133.   }
  134.   getType () {
  135.     return this.type
  136.   }
  137.   get () {
  138.     return {
  139.       type: this.type,
  140.       iq: this.iq,
  141.       name: this.name
  142.     }
  143.   }
  144. }
  145.  
  146. i = 0
  147. let KatrineBrewster
  148. start = process.hrtime.bigint()
  149. while (i < ITERATION) {
  150.   KatrineBrewster = new Human3({ iq: 154, name: 'KatrineBrewster' })
  151.   i++
  152. }
  153. end = process.hrtime.bigint()
  154. let classExampleTime = end - start
  155.  
  156. console.info(
  157.   `CL: ${closureExampleTime}, FN: ${funcExampleTime}, CLASS: ${classExampleTime} nanosec.`
  158. )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top