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);

asked Jul 18, 2019 at 15:49
\$\endgroup\$
3
  • \$\begingroup\$ Is this real life or for school? \$\endgroup\$ Commented Jul 18, 2019 at 16:03
  • \$\begingroup\$ it's for work and the function should have the best performance in all browsers \$\endgroup\$ Commented Jul 18, 2019 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\$ Commented Jul 18, 2019 at 16:20

1 Answer 1

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
answered Jul 18, 2019 at 18:07
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.