Extract file extension

JavaScript performance comparison

Test case created by VisioN

Info

Comparison of different approaches to get file extension in JavaScript

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var filename = "name.with.many.dots.myext";
};
</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
VisioN #1 (long)
var extension = filename.substr((Math.max(0, filename.lastIndexOf(".")) || Infinity) + 1);
pending…
VisioN #2 (short)
var extension = filename.substr((~-filename.lastIndexOf(".") >>> 0) + 2);
pending…
Tom
var extension = (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
pending…
wallacer (popular)
var extension = filename.split(".").pop();
pending…
PhiLho
var extension = /^.+\.([^.]+)$/.exec(filename);
extension = extension == null ? "" : extension[1];
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