Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4100990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:38:18+00:00 2026-05-20T20:38:18+00:00

I am stuck using an AJAX library from about 5 years ago in this

  • 0

I am stuck using an AJAX library from about 5 years ago in this project, and it had some issues with parsing dates in JSON. I wound up rewriting its parse function to use a single regex:

return eval('(' + (enableDateParsing ? text.replace(/"(?:\\)?\/Date\((.*?)\)(?:\\)?\/"/g, "new Date($1)") : text) + ')');

This works really well, but I thought I could get a speed up if I used native JSON parsing in IE8 / chrome / ff, so I added this bit:

if (typeof JSON !== 'undefined' && typeof JSON.parse !== 'undefined') {     
        var nativeJsonDateParseRegex = /\/Date\(.*?\)\//g;       
        return JSON.parse(text, function (key, value) {
            if (AjaxPro.enableDateParsing && typeof value === 'string' && value.match(nativeJsonDateParseRegex)) 
            {
                value = new Date(parseInt(value.substr(6)));
            }                       
            return value;         
        });        
    } 
else // revert to eval for ie6/ie7

The reviver callback will execute once for each JSON property returned, so it has to be very fast. During a profile I’ve seen it’s been called 170484 times, but still runs pretty fast (131.237ms). Any ideas on how to make it faster, or is this the best you can do without serious tweaking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:38:19+00:00Added an answer on May 20, 2026 at 8:38 pm

    Your code contains a lot of constant conditions, you’ll be fine with checking once whether native JSON is supported or not.

    Suggestions:

    • check for native JSPN support at page load, and add the right function accordingly.
    • Drop the global flag from the regex if you do not need it
    • Drop regular expressions if possible, if every date always starts with “/Date(“, search for it. It’s much faster (see benchmark at jsperf.com)
    • todo: check whether parseInt can be replaced with an other method to get rid of the trailing )/.
    • If AjaxPro.enableDateParsing is a constant, you can remove if from AjaxPro.jsonParse and and make it a condition like the check for native JSON

    Code without RE:

    if (typeof JSON !== 'undefined' && typeof JSON.parse !== 'undefined') {
        AjaxPro.nativeJsonDateParseRegex = /\/Date\(.*?\)\//g;
        AjaxPro.dateFunc = function(key, value) {
           if (typeof value === "string" && !value.indexOf("/Date(")) {
               return new Date(value.substring(6, value.length-2));
           }
           return value;
        };
        AjaxPro.jsonParse = function(text) {
            if (AjaxPro.enableDateParsing) {
                return JSON.parse(text, AjaxPro.dateFunc);
            }
            return JSON.parse(text);
        };
    } else // revert to eval for ie6/ie7
    

    This should be highly optimized. You might want to run some more test on your own in multiple browsers. Maybe checking for a property of a string is faster than checking its type (doubt it), thing like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to grab some data from the database using JQuery AJAx, and
I am attempting to use livequery. I unfortunately am stuck using jquery 1.2.6. This
I am stuck with using Dojo to accomplish this. Basically what I am trying
I'm using simplehtmldom to parse html and I'm stuck in parsing plaintext located outside
I am using c# decimal for high accuracy project but was stuck at the
I am using Ajax to retrieve the data from server as below based on
I'm stuck trying to solve this. I'm plotting some information that I have in
I'm building Wordpress website where all content pages are loaded using Ajax. This is
I've done some jQuery in the past, but I am completely stuck on this.
I'm stuck: I'm trying to submit a form using AJAX, but I can't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.