Split array vs indexOf

Benchmark created by gorhill on


Description

Testing to confirm the fastest way to pop the tld off a hostname. My hunch is that lastIndexOf() is faster, as opposed to the often seen split('.').pop().

split('.') causes an array to be create, meaning memory alloc/dealloc, etc. I expect split('.') to be slower.

Setup

function tld_split(hostname) {
      return hostname.split('.').pop();
    }
    
    function tld_lastIndexOf(hostname) {
      var pos = hostname.lastIndexOf('.');
      if (pos < 0) {
        return hostname;
      }
      return hostname.slice(pos + 1);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
tld_split
var tld = tld_split('www.example.com');
ready
tld_lastIndexOf
var tld = tld_lastIndexOf('www.example.com');
ready

Revisions

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