We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to increase the speed of my code execution time?

Hari1
Mega Sage

Hi,

My code execution time on a average of 15-18 seconds. I need to decrease my code execution time to less than 5 seconds. Below is my code.

var usedByOrgVal = "All";

 var arrFinCount = [];

 var arr = [];

 if (usedByOrgVal == "All") {
     arr = ["operational_status!=6", "operational_status!=6^owned_by!=null", "operational_status!=6^u_in_coverage=Yes", "operational_status!=6^u_complete=Yes", "operational_status!=6^u_correct=Yes", "operational_status!=6^u_in_coverage=No", "operational_status!=6^u_complete=No", "operational_status!=6^u_correct=No", "nameISNOTEMPTY^operational_status!=6^u_resp_orgISNOTEMPTY^NQnameISNOTEMPTY^operational_status!=6^u_resp_orgISEMPTY^assetISEMPTY"];
     filterList(arr);
 }

 function filterList(arrVal) {
     for (var i = 0; i < arrVal.length; i++) {
         var cal = new GlideRecord("cmdb_ci_server");
         cal.addEncodedQuery(arrVal[i]);
         cal.query();
         arrFinCount.push(cal.getRowCount());
     }
 }

 gs.info("arrFinCount: " + JSON.stringify(arrFinCount));

 var obj = {
     Ownership: [{
         Value: arrFinCount[1],
         Percentage: isNaN(((arrFinCount[1] / arrFinCount[0]) * 100).toFixed(2)) ? "0.00" : ((arrFinCount[1] / arrFinCount[0]) * 100).toFixed(2),
     }],
     Coverage: [{
         InCoverage: arrFinCount[2],
         OutOfCoverage: arrFinCount[5],
         Percentage: isNaN(((arrFinCount[2] / arrFinCount[0]) * 100).toFixed(2)) ? "0.00" : ((arrFinCount[2] / arrFinCount[0]) * 100).toFixed(2),
     }],
     Completeness: [{
         Complete: arrFinCount[3],
         NotComplete: arrFinCount[6],
         Percentage: isNaN(((arrFinCount[3] / arrFinCount[0]) * 100).toFixed(2)) ? "0.00" : ((arrFinCount[3] / arrFinCount[0]) * 100).toFixed(2),
     }],
     Correctness: [{
         Correct: arrFinCount[4],
         NotCorrect: arrFinCount[7],
         Percentage: isNaN(((arrFinCount[4] / arrFinCount[0]) * 100).toFixed(2)) ? "0.00" : ((arrFinCount[4] / arrFinCount[0]) * 100).toFixed(2),
     }],
     ActiveServerCount: [{
         Count: arrFinCount[8]
     }],
 };

 gs.info("obj:" + JSON.stringify(obj));

 return obj;

Can anyone please provide any solution for the above code.

10 REPLIES 10