Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created August 9, 2018 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nolanlawson/7919d6dbe6c44556778ea9b2118985a7 to your computer and use it in GitHub Desktop.
Save nolanlawson/7919d6dbe6c44556778ea9b2118985a7 to your computer and use it in GitHub Desktop.
Test polymorphism
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test polymorphism</title>
</head>
<body>
<h1>Test polymorphism</h1>
<pre></pre>
<script>
(function () {
function randoInt (start, end) {
return start + Math.ceil(Math.random() * (end - start))
}
function randoString () {
return btoa(Math.random())
}
function randoFloat () {
return Math.random()
}
function randoObject1 () {
return {
a: randoString(),
b: randoInt(),
c: randoFloat(),
d: randoString(),
e: randoFloat(),
f: randoFloat()
}
}
function randoObject2 () {
return {
z: randoString(),
y: randoString(),
x: randoString(),
w: randoInt(),
v: randoString(),
u: randoString()
}
}
function randoObject3 () {
return {
alpha: randoString(),
beta: randoString(),
gamma: randoString(),
delta: randoInt(),
epsilon: randoString(),
zeta: randoString()
}
}
function randoObject4 () {
return {
alpha: randoString(),
bravo: randoString(),
charlie: randoString(),
delta: randoInt(),
echo: randoString(),
foxtrot: randoString()
}
}
function hotFunction(obj) {
var res1 = ''
for (var key in obj) {
res1 += obj[key]
}
var res2 = ''
for (var key in obj) {
res2 += obj[key]
}
var res3 = ''
for (var key in obj) {
res3 += obj[key]
}
return res1 + res2 + res3
}
function callsite1 () {
hotFunction(randoObject1())
}
function callsite2 () {
hotFunction(randoObject2())
}
function callsite3 () {
hotFunction(randoObject3())
}
function callsite4 () {
hotFunction(randoObject4())
}
performance.mark('start')
for (var i = 0; i < 100000; i++) {
switch (i % 4) {
case 0:
callsite1()
break
case 1:
callsite2()
break
case 2:
callsite3()
break
case 3:
default:
callsite4()
break
}
}
performance.mark('end')
performance.measure('total', 'start', 'end')
var duration = performance.getEntriesByName('total')[0].duration
document.querySelector('pre').innerHTML = 'Total time: ' + duration + 'ms'
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment