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

Can you provide a code example on how to use it? I am using the above script in the UI Builder.

See the following documentation:

 

CreateCustomIndex.html

 

Good luck.

Juhi Poddar
Kilo Patron

Hello @Hari1 

I have tried to optimize the script by adding GlideAggregate instead of GlideRecord and simplifying the calculation.

Try this script:

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 ga = new GlideAggregate("cmdb_ci_server");
        ga.addEncodedQuery(arrVal[i]);
        ga.addAggregate("COUNT");
        ga.query();
        if (ga.next()) {
            arrFinCount.push(ga.getAggregate("COUNT")); // Get the count
        }
    }
    gs.info("arrFinCount: " + JSON.stringify(arrFinCount));

    var arrfin = [];
    if (arrFinCount[0] != 0) {
        for (var j = 0; j < 3; j++) {
            arrfin[j] = ((arrFinCount[j + 1] / arrFinCount[0]) * 100).toFixed(2);
        }
    } else {
        arrfin = ["0.00", "0.00", "0.00", "0.00"];
    }

    var obj = {
        Ownership: [{
            Value: arrFinCount[1],
            Percentage: arrfin[0],
        }],
        Coverage: [{
            InCoverage: arrFinCount[2],
            OutOfCoverage: arrFinCount[5],
            Percentage: arrfin[1],
        }],
        Completeness: [{
            Complete: arrFinCount[3],
            NotComplete: arrFinCount[6],
            Percentage: arrfin[2],
        }],
        Correctness: [{
            Correct: arrFinCount[4],
            NotCorrect: arrFinCount[7],
            Percentage: arrfin[3],
        }],
        ActiveServerCount: [{
            Count: arrFinCount[8]
        }],
    };
    gs.info("obj:" + JSON.stringify(obj));
}

Result:

JuhiPoddar_0-1737139108105.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Try this:

function processServerData(usedByOrgVal) {
    if (usedByOrgVal === undefined) {
        usedByOrgVal = "All";
    }
    var sw = new GlideStopWatch();
    var queries = [
        "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"
    ];

    if (usedByOrgVal !== "All") {
        return {}; // Or handle different case if needed
    }

    var arrFinCount = [];
    for (var i = 0; i < queries.length; i++) {
        var count = 0;
        var cal = new GlideRecord("cmdb_ci_server");
        cal.addEncodedQuery(queries[i]);
        cal.query();
        while (cal.next()) {
            count++;
        }
        arrFinCount.push(count);
    }

    function calculatePercentage(numerator, denominator) {
        return isNaN(((numerator / denominator) * 100).toFixed(2)) ? "0.00" : ((numerator / denominator) * 100).toFixed(2);
    }

    var obj = {
        Ownership: [{
            Value: arrFinCount[1],
            Percentage: calculatePercentage(arrFinCount[1], arrFinCount[0]),
        }],
        Coverage: [{
            InCoverage: arrFinCount[2],
            OutOfCoverage: arrFinCount[5],
            Percentage: calculatePercentage(arrFinCount[2], arrFinCount[0]),
        }],
        Completeness: [{
            Complete: arrFinCount[3],
            NotComplete: arrFinCount[6],
            Percentage: calculatePercentage(arrFinCount[3], arrFinCount[0]),
        }],
        Correctness: [{
            Correct: arrFinCount[4],
            NotCorrect: arrFinCount[7],
            Percentage: calculatePercentage(arrFinCount[4], arrFinCount[0]),
        }],
        ActiveServerCount: [{
            Count: arrFinCount[8]
        }],
    };

    gs.info("arrFinCount: " + JSON.stringify(arrFinCount));
    gs.info("obj: " + JSON.stringify(obj));
	gs.print('GlideRecord took ' + sw);
    return obj;
}

// Usage
var result = processServerData();

 

Hi @Sheldon  Swift, The above code took 1 minute 26 seconds to execute. I want the execution time to be within 5 seconds. The record data returning back from the query is 80-50 thousand records.

Below is the snapshot. 

Hari1_0-1737182115912.png