Skip to content

Instantly share code, notes, and snippets.

@eduardoacskimlinks
Forked from JobLeonard/index.html
Created February 3, 2022 10:20
Show Gist options
  • Save eduardoacskimlinks/ccc21879bfeca48a8807f4b662e7acce to your computer and use it in GitHub Desktop.
Save eduardoacskimlinks/ccc21879bfeca48a8807f4b662e7acce to your computer and use it in GitHub Desktop.
Benchmark extracting hostname from URL (http://jsbench.github.io/#ccc21879bfeca48a8807f4b662e7acce) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Benchmark extracting hostname from URL</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
suite.add("const urls = ['https://www.webdesignerforum.co.uk/',", function () {
const urls = ['https://www.webdesignerforum.co.uk/',
'https://bing.com',
'http://www.google.com',
'https://codepen.io/'
]
// split url
for (let i = 0; i < 1024; i++) {
const url = gettingTheHostNameFromURL("https://www.thebump.com/topics/getting-pregnant")
}
"https://www.thebump.com/topics/getting-pregnant"
function gettingTheHostNameFromURL(websiteURL) {
console.log('gettingTheHostNameFromURL', websiteURL)
let getTheHostName;
if (websiteURL.indexOf("//") > -1) {
getTheHostName = websiteURL.split('/')[2];
} else {
getTheHostName = websiteURL.split('/')[0];
}
getTheHostName = getTheHostName.split(':')[0];
getTheHostName = getTheHostName.split('?')[0];
return getTheHostName;
}
});
suite.add("create dom item", function () {
// create dom item
let t = [];
for (let i = 0; i < 1024; i++) {
const url = getDomainFromUrl("https://www.thebump.com/topics/getting-pregnant")
}
function getDomainFromUrl(url, considerNoProtocolAsRelative = true) {
// Gets the domain/hostname from a url by creating a tmp "a" object
const a = document.createElement("a")
// a.hostname will return the hostname of the current page if no protocol is present in the url.
a.href = considerNoProtocolAsRelative ? url : url
try {
// Some invalid urls like "http://javascript:void(0)" would make a.hostname crash on IE and Edge.
return a.hostname
} catch (error) {
return ""
}
}
});
suite.add("Url matching", function () {
// Url matching
let t = [];
for (let i = 0; i < 1024; i++) {
const url = domain_from_url("https://www.thebump.com/topics/getting-pregnant")
}
function domain_from_url(url) {
var result
var match
if (match = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n\?\=]+)/im)) {
result = match[1]
if (match = result.match(/^[^\.]+\.(.+\..+)$/)) {
result = match[1]
}
}
return result
}
});
suite.add("URL matching hostname", function () {
// URL matching hostname
let t = [];
for (let i = 0; i < 1024; i++) {
const url = getHostName("https://www.thebump.com/topics/getting-pregnant")
}
function getHostName(url) {
var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
return match[2];
}
else {
return null;
}
}
});
suite.add("URL matching hostname v2", function () {
// URL matching hostname v2
let t = [];
for (let i = 0; i < 1024; i++) {
const url = getHostName2("https://www.thebump.com/topics/getting-pregnant")
}
function getHostName2(url) {
var match = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n\?\=]+)/im);
if (match != null && match.length > 1 && typeof match[1] === 'string' && match[1].length > 0) {
return match[1];
}
else {
return null;
}
}
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Benchmark extracting hostname from URL");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment