Date.parse js-iso8601

Benchmark created by Harold on


Preparation HTML

<script type="text/javascript">
/**
 * Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601>
 * © 2011 Colin Snover <http://zetafleet.com>
 * Released under MIT license.
 */
(function (Date, undefined) {
    var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
    Date.parse1 = function (date) {
        var timestamp, struct, minutesOffset = 0;

        // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
        // before falling back to any implementation-specific date parsing, so that’s what we do, even if native
        // implementations could be faster
        //              1 YYYY                2 MM       3 DD           4 HH    5 mm       6 ss        7 msec        8 Z 9 ±    10 tzHH    11 tzmm
        if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
            // avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
            for (var i = 0, k; (k = numericKeys[i]); ++i) {
                struct[k] = +struct[k] || 0;
            }

            // allow undefined days and months
            struct[2] = (+struct[2] || 1) - 1;
            struct[3] = +struct[3] || 1;

            if (struct[8] !== 'Z' && struct[9] !== undefined) {
                minutesOffset = struct[10] * 60 + struct[11];

                if (struct[9] === '+') {
                    minutesOffset = 0 - minutesOffset;
                }
            }

            timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
        }
        else {
            timestamp = origParse ? origParse(date) : NaN;
        }

        return timestamp;
    };
}(Date));
</script>

<script type = "text/javascript" > 

(function (Date) {

    var origParse = Date.parse,

        // ES5 15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
        // before falling back to any implementation-specific date parsing, so that's what we do, even if native
        // implementations could be faster
        //                               1 YYYY                 2 MM        3 DD            4 HH     5 mm        6 ss        7 msec         8 Z 9 +-  10 tzHH     11 tzmm
        isoDateExpression = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/,

        numericKeys = [1, 4, 5, 6, 7, 10, 11];

    Date.parse2 = function (date) {

        var i, k, struct, timestamp,
            minutesOffset = 0;

        if (struct = isoDateExpression.exec(date)) {

            // avoid NaN timestamps caused by "undefined" values being passed to Date.UTC
            for (i = 0, k; (k = numericKeys[i]); ++i) {
                struct[k] = +struct[k] || 0;
            }

            // allow undefined days and months
            struct[2] = (+struct[2] || 1) - 1;
            struct[3] = +struct[3] || 1;

            if (struct[8] !== 'Z' && struct[9] !== void 0) {
                minutesOffset = struct[10] * 60 + struct[11];

                if (struct[9] === '+') {
                    minutesOffset = -minutesOffset;
                }
            }

            timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]);
        } else {
            timestamp = origParse ? origParse(date) : NaN;
        }

        return timestamp;
    };
}(Date));
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Original js-iso8601
Date.parse1('1970-01-01 UTC');  
Date.parse1('2001 UTC');        
Date.parse1('2001-02 UTC');     
Date.parse1('2001-02-03 UTC');  
                                
Date.parse1('asdf');    
Date.parse1('1970-as-df');
Date.parse1('19700101'); 

Date.parse1('2001-02-03T05:05');         
Date.parse1('2001-02-03T05:05:06');                    
Date.parse1('2001-02-03T05:05:06.007');  
                                         
Date.parse1('2001-02-03T04:05Z');        
Date.parse1('2001-02-03T04:05:06Z');     
Date.parse1('2001-02-03T04:05:06.007Z'); 

Date.parse1('2001-02-03T04:05-00:00');                                         
Date.parse1('2001-02-03T04:05:06-00:00');    
Date.parse1('2001-02-03T04:05:06.007-00:00');

Date.parse1('2001-02-03T04:05+00:00');       
Date.parse1('2001-02-03T04:05:06+00:00');    
Date.parse1('2001-02-03T04:05:06.007+00:00');          
ready
js-iso8601 With RegExp outside Date.parse
Date.parse2('1970-01-01 UTC');  
Date.parse2('2001 UTC');        
Date.parse2('2001-02 UTC');     
Date.parse2('2001-02-03 UTC');  
                                
Date.parse2('asdf');    
Date.parse2('1970-as-df');
Date.parse2('19700101'); 

Date.parse2('2001-02-03T05:05');         
Date.parse2('2001-02-03T05:05:06');                    
Date.parse2('2001-02-03T05:05:06.007');  
                                         
Date.parse2('2001-02-03T04:05Z');        
Date.parse2('2001-02-03T04:05:06Z');     
Date.parse2('2001-02-03T04:05:06.007Z'); 

Date.parse2('2001-02-03T04:05-00:00');                                         
Date.parse2('2001-02-03T04:05:06-00:00');    
Date.parse2('2001-02-03T04:05:06.007-00:00');

Date.parse2('2001-02-03T04:05+00:00');       
Date.parse2('2001-02-03T04:05:06+00:00');    
Date.parse2('2001-02-03T04:05:06.007+00:00');          
ready

Revisions

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

  • Revision 1: published by Harold on