Filename from URL

JavaScript performance comparison

Test case created by

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 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();
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.

0 Comments