Filename from URL

JavaScript performance comparison

Test case created


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

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 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();
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. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment