Last active
July 30, 2019 07:44
-
-
Save hoangtranson/24449bd4f9834fe0e5ab691803aef04b to your computer and use it in GitHub Desktop.
Test performance with prototype
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Prototype Performance</title> | |
<meta charset="UTF-8" /> | |
</head> | |
<body> | |
<div id="app" style="margin:100px"></div> | |
<script> | |
function FuncNoPrototype() { | |
this.value = 0; | |
this.increment = function () { | |
this.value++; | |
console.log("printed: ", this.value); | |
}; | |
} | |
const t0 = (new Date()).getTime(); | |
for (let i = 0; i < 1000; i++) { | |
const instanceNoPrototype = new FuncNoPrototype(); | |
instanceNoPrototype.increment(); | |
} | |
const t1 = (new Date()).getTime(); | |
function FuncPrototype() { | |
this.value = 0; | |
} | |
FuncPrototype.prototype.increment = function () { | |
this.value++; | |
console.log("printed: ", this.value); | |
}; | |
const t2 = (new Date()).getTime(); | |
for (let i = 0; i < 1000; i++) { | |
const instancePrototype = new FuncPrototype(); | |
instancePrototype.increment(); | |
} | |
const t3 = (new Date()).getTime(); | |
document.getElementById("app").innerHTML = ` | |
<h1>time to create 1000 instances</h1> | |
<div>without prototype: ${t1 - t0}</div> | |
<div>with prototype: ${t3 - t2}</div> | |
`; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment