12

I've been wondering this for a while, so it's time to put this question out there. How does Schema.describeSObjects perform when compared to individual calls to SObjectType.getDescribe?

For example, which of these is faster? By how much?

static Map<String, SObjectType> globalDescribe = Schema.getGlobalDescribe();
public static List<DescribeSObjectResult> getIndividualDescribes(List<String> types)
{
    List<DescribeSObjectResult> describes = new List<DescribeSObjectResult>();
    for (String sObjectType : types)
        describes.add(globalDescribe.get(sObjectType).getDescribe());
    return describes;
}
public static List<DescribeSObjectResult> getBulkDescribes(List<String> types)
{
    return Schema.describeSObjects(types);
}
15

Getting the individual describes is significantly faster. Depending on your situation, it is anywhere from 14 to 850 times as fast!

Describing Many Objects Once

The first continuum I profiled is the situation where you have many objects to describe and make one call to describe all of them.

The individual getDescribe approach can describe anywhere from 5 to 11 objects per millisecond, whereas the bulk describeSObjects approach takes anywhere from 2 to 3.5 milliseconds per object. In this continuum, individual describes perform roughly 14 to 30 times better than the bulk describe method.

Here is the amount of CPU time consumed for each approach to describe a given number of sObjects once:

Objects  |  Individual  |    Bulk  |  Multiplier
================================================
      5  |         1.1  |    17.5  |        15.9
     10  |         1.5  |    24.6  |        16.4
     15  |         2.1  |    29.7  |        14.1
     20  |         2.6  |    38.7  |        14.9
     25  |         2.8  |    71.4  |        25.5
     30  |         3.1  |    88.7  |        28.6
     35  |         3.3  |   107.7  |        32.6
     40  |         4.6  |   117.2  |        25.5
     45  |         4.3  |   133.4  |        31.0
     50  |         4.7  |   144.3  |        30.7
     55  |         5.2  |   151.4  |        29.1
     60  |         5.9  |   153.7  |        26.1
     65  |         6.2  |   156.5  |        25.2
     70  |         6.8  |   167.3  |        24.6
     75  |         7.3  |   174.7  |        23.9
     80  |         7.6  |   184.7  |        24.3
     85  |         7.9  |   192.4  |        24.4
     90  |         9.0  |   193.0  |        21.4
     95  |         8.7  |   198.1  |        22.8
    100  |         9.1  |   209.7  |        23.0

Describing One Object Many Times

The second continuum I profiled is the situation where you describe one object many times, for instance if you have describes in a loop somewhere in your code.

The individual getDescribe approach can describe an object anywhere from 17 to 33 times per second, whereas the bulk describeSObjects approach takes anywhere from 25 to 29 milliseconds per describe. In this continuum, individual describes perform hundreds of times better than the bulk describe method.

Here is the amount of CPU time consumed for each approach to describe one object (User) a given number of times:

Iterations  |  Individual  |     Bulk  |  Multiplier
====================================================
        25  |         1.5  |    719.7  |       479.8
        50  |         2.6  |   1408.4  |       541.7
        75  |         2.9  |   2147.9  |       740.7
       100  |         4.3  |   2782.4  |       647.1
       125  |         4.4  |   3667.9  |       833.6
       150  |         5.2  |   4171.3  |       802.2
       175  |         6.1  |   4640.4  |       760.7
       200  |         7.3  |   5167.1  |       707.8
       225  |         7.2  |   5887.2  |       817.7
       250  |         7.8  |   6618.9  |       848.6
       275  |         8.7  |   7175.3  |       824.7
       300  |         9.0  |   7537.6  |       837.5
       325  |         9.8  |   8328.8  |       849.9
       350  |        11.1  |   9090.1  |       818.9
       375  |        12.9  |   9423.5  |       730.5
       400  |        12.3  |  10422.8  |       847.4
       425  |        13.5  |  10716.1  |       793.8
       450  |        13.6  |  11431.6  |       840.6
       475  |        14.5  |  12063.8  |       832.0
       500  |        14.8  |  12977.7  |       876.9
  • 2
    I know it won't be dynamic, but what if you compare Schema.getGlobalDescribe().get('Account') with Account.sObjectType.getDescribe() or Schema.SObjectType.Account? What do you think would be faster ? – Cloud Ninja Dec 20 '16 at 23:17
  • Sounds like a separate question! One which I would also be happy to profile if no one beats me to it. Note that if you are calling Schema.getGlobalDescribe many times that will affect performance, and the first iteration is slower than the rest. I think SObjectType.Account is fastest, but I've never checked. – Adrian Larson Dec 20 '16 at 23:22
  • @AdrianLarson - Sorry, confused describesObjects with the global call – Eric Dec 20 '16 at 23:26
0

I re-ran the this scenario and looks like Schema.describeSObjects seems to have better performance now when compared to SObjectType.getDescribe for individual objects, I'm assuming salesforce must have optimised this flow.

Below is the profiler trace for 'Account' and 'Product2' objects.

enter image description here

This site is temporarily in read only mode and not accepting new answers.

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