Regex vs Split

JavaScript performance comparison

Test case created by Josh Vermaire

Info

Quick regex vs split tests on a date string, only looking for year/month.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var date = "1988-12-02 08:00:00";
    var REGEX = /(\d{4})-(\d{2})/;
};
</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
Regex
var dateArray, day, month, year;
dateArray = /(\d{4})-(\d{2})/.exec(date);
year = dateArray[1];
month = dateArray[2];
pending…
Cached Regex
var dateArray, day, month, year;
dateArray = REGEX.exec(date);
year = dateArray[1];
month = dateArray[2];
pending…
Split
var dateArray, day, month, year;
dateArray = date.split(' ')[0].split('-');
year = dateArray[0];
month = dateArray[1];
day = dateArray[2];
pending…

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

Compare results of other browsers

0 comments

Add a comment