Loading ...
Sorry, an error occurred while loading the content.

1311Why we have to waist our time for Date parsing?

Expand Messages
  • Andrea Giammarchi
    Aug 7, 2009
    • 0 Attachment
      JSON.parse function accepts an extra argument which main usage is to
      transform strings into Dates, if encountered.
      For performances reason this operation will cost a callback for each item to
      "walk", a regexp check for each string encountered in each item, eventually
      the creation of a new Date(Date.UTC(arguments ... )) to return.

      All this stuff could be simplified simply adding this piece of code the
      instant it is evaluated:

      // fast date regeneration via ISO RegExp from WebReflection
      j = eval('(' + text.replace(

      /(^|[:,\[])"(\d{4})(-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|((\+|-)(\d{2}):(\d{2}))))?)?)?"/g,
      "$1new Date(Date.UTC($2,$4-1,$6,$8,$9,$11,$13+0))"
      ) + ')');

      Performance speaking, it is not possible to compare the real gap between
      this solution and the "manual one" 'cause more date objects we have, more
      this solution will make the difference and the operation fast.

      The problem is that every native JSON.parse will not accept a transformed
      string before its call so in these browsers we need to convert manually. It
      is possible in any case to test, at least, the JSON string in orther to
      understand if the Date parsing is required.

      JSON.parse = (function(parse){
      return function(value){
      var transformDates = aboveISORegExp.test(value);
      value = parse.call(JSON, value);
      if(transformDates)
      walk(value, function(str){ ... the callback to check and transform
      str in Date ... })
      };
      })(JSON.parse);

      I would like to know your opinion about this, thanks.

      Regards


      [Non-text portions of this message have been removed]
    • Show all 33 messages in this topic