Split array vs indexOf (v2)

Revision 2 of this benchmark created 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);
    }
    
    function tld_lastIndexOf_slice(hostname) {
      return hostname.slice(hostname.lastIndexOf('.') + 1);
    }
    
    function tld_lastIndexOf_substring(hostname) {
      return hostname.substring(hostname.lastIndexOf('.') + 1);
    }
    
    function tld_lastIndexOf_substr(hostname) {
      return hostname.substr(hostname.lastIndexOf('.') + 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
tld_lastIndexOf_slice
var tld = tld_lastIndexOf_slice('www.example.com');
ready
tld_lastIndexOf_substring
var tld = tld_lastIndexOf_substring('www.example.com');
ready
tld_lastIndexOf_substr
var tld = tld_lastIndexOf_substr('www.example.com');
ready

Revisions

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