Created
November 19, 2019 12:08
-
-
Save Edorka/fbfb0778c859d8f518f0508414d3e6a2 to your computer and use it in GitHub Desktop.
Testing constact vs attribute access performance
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
const range = n => Array.from(Array(n).keys()); | |
function caseA() { | |
const parentObject = { myObject: { items: range(500000) } }; | |
let hrstart = process.hrtime(); | |
let counter = 0; | |
for (let i = 0, len = parentObject.myObject.items.length; i < len; i++) { | |
counter += parentObject.myObject.items[i]; | |
} | |
console.log("caseA total", counter); | |
let hrend = process.hrtime(hrstart); | |
console.info("Execution time (hr): %ds %dms", hrend[0], hrend[1] / 1000000); | |
} | |
function caseB() { | |
const parentObject = { myObject: { items: range(500000) } }; | |
let hrstart = process.hrtime(); | |
let counter = 0; | |
const { myObject } = parentObject; | |
const { items } = myObject; | |
for (let i = 0, len = items.length; i < len; i++) { | |
counter += items[i]; | |
} | |
console.log("caseB total", counter); | |
let hrend = process.hrtime(hrstart); | |
console.info("Execution time (hr): %ds %dms", hrend[0], hrend[1] / 1000000); | |
} | |
caseA(); | |
caseB(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment