Why is Schema.describeSObjects Slower Than Schema.getGlobalDescribe?

Why is Schema.describeSObjects Slower Than Schema.getGlobalDescribe?

On May 1, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Why is Schema.describeSObjects Slower Than Schema.getGlobalDescribe?
Why is Schema.describeSObjects Slower Than Schema.getGlobalDescribe

Question

I wanted to reconfigure my code to use the newer Schema.describeSObjects(types) method, expecting it to be faster than the older Schema.getGlobalDescribe approach. However, after testing, I found that Schema.describeSObjects is significantly slower. Here’s the code I used for comparison:

New DescribeSObjects Method:

String obj = 'Account';
String[] types = new String[]{obj};
System.debug('START DESCRIBE SOBJECTS');

for (Integer i = 0; i < 50; i++) {
    List<Schema.DescribeSobjectResult> results = Schema.describeSObjects(types); 
}
System.debug('FINISH DESCRIBE SOBJECTS');

Old GetGlobalDescribe Method:

String obj1 = 'Account';
System.debug('START DESCRIBE GLOBAL');

for (Integer i = 0; i < 50; i++) {
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
    Schema.SObjectType sobjType = gd.get(obj1); 
    Schema.DescribeSObjectResult describeResult = sobjType.getDescribe(); 
}
System.debug('FINISH DESCRIBE GLOBAL');

Over five runs, the old getGlobalDescribe() method took an average of 337ms for 50 describes, while describeSObjects() averaged 658ms, making it almost twice as slow.

What could explain this discrepancy, and is there a way to improve the performance of these operations?

Answer

In general, the performance of these methods depends on their internal implementation and use case. Here are some insights and solutions:

Join CRS Info Solutions, a leading institute for  Salesforce training in Chennai. Sign up for a free demo session today!!!

1.Possible Explanation

Internally, Schema.describeSObjects(types) may involve more overhead than Schema.getGlobalDescribe(). The latter provides a global map of SObject types, making it more efficient when repeatedly accessing a single object. On the other hand, Schema.describeSObjects(types) is designed to describe multiple objects at once and performs better in bulk use cases.

2.Improving Performance

If you are frequently describing the same objects, caching the results can significantly improve performance. Here’s an example of how to implement caching:

public static Map<String, SObjectType> globalDescribe {
    get {
        if (globalDescribe == null) {
            globalDescribe = Schema.getGlobalDescribe();
        }
        return globalDescribe;
    }
}

This ensures that Schema.getGlobalDescribe() is called only once per transaction, avoiding repeated operations.

3.Alternative Using Type Class

A faster alternative to both methods is leveraging the Type.forName() method, as shown below:

String obj1 = 'Account';
Long time1 = DateTime.now().getTime();

for (Integer i = 0; i < 50; i++) {
    SObjectType r = ((SObject)Type.forName('Schema', obj1).newInstance()).getSObjectType();
    DescribeSObjectResult d = r.getDescribe();
}
Long time2 = DateTime.now().getTime();
System.debug(time2 - time1);

This approach eliminates the need for global describe or bulk describe operations and is significantly faster, running in 10-15ms for 50 describes in some orgs.

4.Bulk Use Case

While Schema.describeSObjects() may appear slower in this specific scenario, it performs well when describing multiple objects in a single call. For example:

String[] types = new String[]{'Account', 'Contact', 'Opportunity'};
List<Schema.DescribeSObjectResult> results = Schema.describeSObjects(types);

This reduces the overhead of repeated global lookups or instantiations for multiple objects.

Conclusion

In conclusion, while Schema.describeSObjects(types) is slower for single-object lookups, it excels in bulk operations. For best performance, use caching for Schema.getGlobalDescribe() or consider Type.forName() when repeatedly describing individual objects. Always choose the method that aligns with your specific use case and performance requirements.

Accelerate Your Salesforce Career in Chennai

Elevate your Salesforce skills with expert-led training in Chennai, designed for both beginners and professionals. Gain practical experience through real-world projects and master cloud-based CRM solutions with guidance from certified instructors. Unlock new career opportunities and become a Salesforce expert with comprehensive, hands-on learning!

CRS Info Solutions is a leading Salesforce Training in Chennai, offering a comprehensive, real-time project-based curriculum. Our courses cover Salesforce Admin, Developer, Integration, Marketing Cloud, CPQ, and Lightning Web Components (LWC). With expert instructors providing hands-on training, we ensure you’re equipped for real-world challenges. Join us to become a certified Salesforce professional and kickstart your career.

Enroll now for a free demo at CRS Info Solutions, Chennai..!!

Comments are closed.