Created
January 18, 2017 21:59
-
-
Save valdrinkoshi/195c1dfc44ffc164b96c0d6cc1d65530 to your computer and use it in GitHub Desktop.
1000 urls vs docs performance measurement
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> | |
<meta charset="utf-8"> | |
<title>doc vs url</title> | |
<script src="../URL/url.js"></script> | |
</head> | |
<body> | |
<script> | |
let doc; | |
let count = 1000; | |
let t = performance.now(); | |
for (var i = 0; i < count; i++) { | |
getUrl('b', 'http://localhost/'); | |
} | |
console.log(performance.now() - t, 'url time'); | |
t = performance.now(); | |
for (var i = 0; i < count; i++) { | |
getUrlFromDoc('b', 'http://localhost/'); | |
} | |
console.log(performance.now() - t, 'doc time'); | |
t = performance.now(); | |
for (var i = 0; i < count; i++) { | |
getUrlFromDoc('b', 'http://localhost/'); | |
} | |
console.log(performance.now() - t, 'doc time (again)'); | |
function getUrl(url, base) { | |
return new URL(url, base); | |
} | |
function getUrlFromDoc(url, base) { | |
if (!doc) { | |
const t = performance.now(); | |
doc = document.implementation.createHTMLDocument('temp'); | |
doc.__base = doc.createElement('base'); | |
doc.head.appendChild(doc.__base); | |
doc.__anchor = doc.createElement('a'); | |
console.log(performance.now() - t, 'doc creation time'); | |
} | |
doc.__base.href = base; | |
doc.__anchor.href = url; | |
return doc.__anchor.href || url; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment