8

This is not a question, but one of my findings after spending some significant time on Lightning framework.

Problem : I was querying ~3k accounts and related child objects and was building a wrapper class to return the response back to the lightning app. The code looked something like this

Public Class AccountQueryController{

    @AuraEnabled

    Public Static List<AccountWrapper> queryAllAccounts(){

        List<AccountWrapper> accountWrapperList = New List<AccountWrapper>();

        List<Account> myAccounts = [Select id, Name, BillingState.
                                           (Select id
                                              From Contacts)
                                      From Account];

        if(!myAccounts.isEmpty()){
            for(Account currentAccount : myAccounts){
                accountWrapperList.add(new AccountWrapper (..) );
            }
        }

        return accountWrapperList;
    }

    Public Class AccountWrapper{
        @AuraEnabled
        Account currentAccount;

        @AuraEnabled
        Integer noOfContacts;

        @AuraEnabled
        Boolean isPartnerAccount;

        @AuraEnabled
        //Few more variables

        Public AccountWrapper(Account currentAccount, Integer noOfContacts, Boolean isPartnerAccount, ..){
            this.currentAccount = currentAccount;
            ....
        }
    }
}

It was taking close to 8 Seconds to load the component and which from user experience is quite a lot of time.

Solution After exploring chrome JS console for event timelines and SF developer console to understand the server process time for query handling and returning the response, I realised AURA framework was taking a lot of time to create the auraBindings for wrapper class variables. There were plenty of aura statements in the logs.

Just to experiment with the component performance, I decided to modify the response type and return a JSON object string, instead of an object list itself.

To my surprise, it improved the component performance quite significantly and brought it down to ~3 seconds (Which it not too bad).

Public Class AccountQueryController{

    @AuraEnabled

    Public Static List<AccountWrapper> queryAllAccounts(){

        List<AccountWrapper> accountWrapperList = New List<AccountWrapper>();

        List<Account> myAccounts = [Select id, Name, BillingState.
                                           (Select id
                                              From Contacts)
                                      From Account];

        if(!myAccounts.isEmpty()){
            for(Account currentAccount : myAccounts){
                accountWrapperList.add(new AccountWrapper (..) );
            }
        }

        return JSON.serialize(accountWrapperList);
    }

    Public Class AccountWrapper{
        Account currentAccount;
        Integer noOfContacts;
        Boolean isPartnerAccount;
        //Few more variables

        Public AccountWrapper(Account currentAccount, Integer noOfContacts, Boolean isPartnerAccount, ..){
            this.currentAccount = currentAccount;
            ....
        }
    }
}

Let me know if anybody has faced anything similar. Cheers.

4
  • 1
    It might be because of some extra work aura framework has to do for binding. I did not check from performance perspective. Could you please tell how you measured that component is taking 8 seconds? Commented May 8, 2017 at 5:06
  • 1
    Have you tried this in Summer 17 (e.g. a Sandbox?) I hear it's supposed to be much better.
    – sfdcfox
    Commented May 8, 2017 at 5:09
  • I used the JS console (Network) to check how long did it take to return the call back. @KiranMachhewar Commented May 8, 2017 at 11:49
  • After switching to string as response type and JS object at client side, performance has improved drastically @sfdcfox Commented May 8, 2017 at 11:50

1 Answer 1

3

Salesforce community optimizer chrome plugin can actually breakup and help identify slowness in your components. On Server calls there is a section other which can help tell how much time it takes to serialize return type of your aura enabled class method.

  1. Salesforce did a review of our code and they said List<SrappperClasses> is causing high serializing and de-serializing time.They asked us to send sObject instances with just relevant fields and manipulate UI using a java script object

  2. Also if your markup contains lot of custom labels(>100) it increases the overall serialization time. they asked to load the entire set of labels as a map and refer that in lightning components instead of directly referring them in markups using labels.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .