Filename from URL

JavaScript performance comparison

Test case created by

Preparation code

Benchmark.prototype.setup = function() {
  var location = "http://www.testsite.test/some/path/to/file.jsp?var1=foo&var2=bar#SomeDeepLink";
  var a= document.createElement('a');
  a.href = location;


Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
myPage() method
function myPage() {
    //TODO: This can be better handled with a RegEx expression
    var whereIam = a.href,
        abreak = whereIam.split("#"),
        fbreak = abreak[0].split("?"),
        fUrl = fbreak[0],
        gUrl = fUrl.split("/");
    Array.prototype.last = function () {
        return this[this.length - 1];
    return gUrl.last();

var result = myPage();
Pathname method
var filename= a.pathname.split('/').pop();
Regex approach
var pattern = /[^\/?#]+(?=$|[?#])/ig;
var result = pattern.exec(a.href);

Compare results of other browsers


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