3
\$\begingroup\$

I have Ajax function with a callback which fetches all the Patient data however I have some perfomance issues and trying to figure out what it might be, does anyone have idea? That's my Code

var patient = new Array();
function sync(arg, callback){   //ajax result 
    $('.loader').show();
    $.ajax({ 
        method: 'GET',
        url: 'sync/active.php',
        dataType: 'json',
        data: arg, // argument schicken und aktualisieren
        success: function(data, status, xhr){
         $('.loader').hide();    
         callback(data);
        // setTimeout(sync, ajaxDelay);
        },
        error: function(xhr, ajaxOptions, thrownError){
            console.log(thrownError);
        }
   });   
}
function onPatientCallback(data) {
        var res = data; 
        for(var i=0; i<res.length;i++){
            for(var key in res[i]){
                var value = res[i][key];
                switch(key){
                    case "id":
                        res[i][key] = parseInt(value);
                        break;
                    case "kundennr":
                        res[i][key] = parseInt(value);  
                        break;
                    case "client":
                        res[i][key] = value;
                        break;
                    case "start":
                        res[i][key] = new Date(value);
                        break;
                    case "end":
                        res[i][key] = new Date(value);
                        break;
                    case "title":
                        res[i][key] = value;
                        break;
                    case "description":
                        res[i][key] = value;
                        break;

                    case "termart":
                        res[i][key] = parseInt(value);
                        break;

                    case "userId":
                        res[i][key] = parseInt(value);
                        break;

                    default:
                        console.log("unbekannter Datentyp "+key);
                }
            }
        }
        patient = res;

}

I use the function to fill the patient variable and call it in another js file like that sync({calling: "patient"}, onPatientCallback);

\$\endgroup\$
  • \$\begingroup\$ Is this real life or for school? \$\endgroup\$ – konijn Jul 18 at 16:03
  • \$\begingroup\$ it's for work and the function should have the best performance in all browsers \$\endgroup\$ – Mike J Jul 18 at 16:08
  • 1
    \$\begingroup\$ Welcome to Code Review! For the sake of reviewers, would you be able to edit your post to include 1. when snyc() and onPatientCallback() are called, and 2. possible HTML corresponding to this code? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Jul 18 at 16:20
2
\$\begingroup\$

From a short review;

  • patient is a global variable, global variables are bad
  • var patient = [] is more idiomatic than var patient = new Array();
  • Comments should be all German or all English (I would go for all English, its the common language of the developers)
  • You can group switch labels, this is valid JavaScript:

            switch(key){
                case "id":
                case "kundennr":
                case "termart":
                case "userId":
                    res[i][key] = parseInt(value);
                    break;
                case "client":
                case "title":
                case "description":
                    res[i][key] = value;
                    break;
                case "start":
                case "end":
                    res[i][key] = new Date(value);
                    break;
                default:
                    console.log("unbekannter Datentyp " + key);
    
  • Your data fields should be either all English or all German (you have userId, but also kundennr), here as well I would go for all English
  • Since value already contains res[i][key] for "client", "title", etc., you should probably comment on that
  • Since this is real, you should not log to console for an unknown data type, you should call a REST service that will log this entry in a database table, and even possibly send out an email to developers
  • var key in Object.keys(res[i]) is safer than var key in res[i], you never know when someone decides to extend Object
  • Your indenting is off, consider using a beautifier
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.