Filename from URL

JavaScript performance comparison

Revision 2 of this test case created by Dmitry Erman

Info

Testing various ways to parse a filename from URL for performance.

Preparation code

 
<script>
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;
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
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();
pending…
Pathname method
var filename= a.pathname.split('/').pop();
pending…
Regex approach
var pattern = /[^\/?#]+(?=$|[?#])/ig;
var result = pattern.exec(a.href);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment