Skip to content

Instantly share code, notes, and snippets.

@valdrinkoshi
Created January 18, 2017 21:59
Show Gist options
  • Save valdrinkoshi/195c1dfc44ffc164b96c0d6cc1d65530 to your computer and use it in GitHub Desktop.
Save valdrinkoshi/195c1dfc44ffc164b96c0d6cc1d65530 to your computer and use it in GitHub Desktop.
1000 urls vs docs performance measurement
<!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