[SalesForce] How efficient is Lightning Data Service when working with multiple records

I've seen quite a few Lightning Data Services posts pop up recently. This question used it to work on a set of records as opposed to a single record. The way it was coded had me worried thinking that it may be terribly inefficient when working on sets of records:

Troubling code sample:

<aura:iteration items="{!v.contactIds}" var="contact">
    <c:ContactComponent contactId="{!contact}"/>
    <hr/>
</aura:iteration>

It passes an ID to a sub component that then uses LDS to retrieve and display the record details to the page. To me, this is the equivalent of a SOQL query in a for loop.

The question is, how accurate is this assumption? Would it be more efficient to use APEX to retrieve the records via a single query as opposed to LDS for each record individually?

The documentation here seems to support my concern with it's brief explanation of the LDS:

At the simplest level, you can think of Lightning Data Service as the Lightning Components version of the Visualforce standard controller.

It's being compared to the standard controller, not the standard set controller.

I'm just curious if anyone has put this to the test.

Best Answer

After doing some testing, I can say that this is very inefficient. Loading more than about 6 records caused a separate loading to occur. While the system did not crash, as I hypothesized originally in my comment, the total loading time was incredibly large. For about 800 records, it took over 20 seconds to load, about 40 records per second. And this was for a field specified by the fields attribute. This is opposed to my PagingSortingDemo, which loads about 52,770 records with 4 fields in three seconds, or about 17,500 records per second, a total increase in speed of about 43,900%.

The moral of the story is that you should only use Lightning Data Service to load one record at a time. Similarly, if you're going to choose to save data, make a list and save them all at once. Lightning Data Service is ideal for things like Quick Actions that update or query the related record (and with even better performance when using the cache). It is not ideal for bulk loading or saving of data.

Hopefully, they'll introduce a bulk-friendly option for both loading and saving data in the future. For now, you should prefer to use Apex Code whenever you need to upload more than a few records at a time.

Related Topic