Lightning Page Load times with optimizations on the Network Calls
Lightning Page Load times with optimizations on the Network Calls
Model-driven apps are highly customized and can pull data from many different sources to satisfy your business needs. When using custom logic to retrieve data from CDS or any other data source, one important factor to consider is – performance. Typically, pro developers use JavaScript to request data using Web API calls like retrieve multiple records for an Entity using the fetch query filters.
These query can be expensive if filters are not applied judiciously and may become expensive. Consider a case where customer wants to ensure if at least one record exists matching the requirement, the retrieve calls can be crafted just to retrieve the top record. The addition of retrieving only top record saves lot of work for the server, as this will stop the scan after fetching the record. This also play a significant role in page load times, in case if there's more than one record in the results for the query is not ordered by any condition.
For example, In the below code snippet , the user is trying to retrieve all the records associated with a particular entity and after the results are retrieved, user checks if at least one record exists and accesses only the first record. This can be easily replaced by retrieving only the top record and access the retrieved record.
Existing Code:
function retrieveMultipleRecords(){
Xrm.WebApi.retrieveMultipleRecords("account", "").then( function (results) {
if (results != null) {
if (results.value.length > 0) {
var bl = true;
}
}
});
}
Simple enhancement to the above code for speedy network calls:
function retrieveMultipleWithTopRecord(){
Xrm.WebApi.retrieveMultipleRecords("account", "?$top=1").then( function (results) {
if (results != null) {
if (results.value.length > 0) {
var bl = true;
}
}
});
}
Results in execution times of the retrieve calls are below. Total number of records for accounts are taken as 100 in this sample. As the number of records be very high, in a realistic scenario, the execution times will be quite expensive and significantly impacts the performance.
Speed up of ~2X is observed in the case of retrieval of only top record instead of retrieving all 100 records in this case.
Summary:
It is always recommended to retrieve only top record if the need is to validate the condition on the existence of at least one record with the given filters. This speeds up the performance significantly.
*This post is locked for comments