hostname regex

Benchmark created by yc on


Preparation HTML

<a href="/foo"></a>

<a href="http://foo.jsperf.com/"></a>

<a href="http://jsperf.com/bar"></a>

<a href="//jsperf.com/bar"></a>

<a href="http://google.com"></a>

<a href="http://reader.google.com"></a>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var x = 0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
hostname/split
jQuery('a').each(function() {

 if (this.hostname !== location.hostname && this.hostname.split('.').slice(-2).join() === location.hostname.split('.').slice(-2).join()) {
  x = 0;
 }

});
ready
just regex
jQuery('a').each(function() {

 var dest = $("#foo").attr("href");

 if (!(new RegExp(location.host, "i")).test(dest)) {
  x = 0;

 }

});
ready
just hostnames
jQuery('a').each(function() {
 if (this.hostname !== location.hostname) {
  x = 0;
 }

});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.