3

Supposing we have a method that executes logic dynamically depending on the SObject type like the following one:

public class TestDescriber {

    public static void callMeManyTimes(SObject record) {

        String sObjectName = record.getSObjectType().getDescribe().getName();

        ...
    }
}

Assuming this method would be called multiples times per transaction and many of those calls will involve the same SObject type, will the getDescribe() calls be expensive enough to justify having a static cache in the class to avoid calling the describer multiple times?

public class TestDescriber {

    private static Map<SObjectType, String> namesPerSObjectType = new Map<SObjectType, String>();

    public static void callMeManyTimes(SObject record) {

        String sObjectName = getSObjectName(record.getSObjectType());

        ...
    }


    private static void getSObjectName(SObjectType sType) {

        if (!namesPerSObjectType.containsKey(sType)) {
            namesPerSObjectType.put(sType, sType.getDescribe().getName());
        }

        return namesPerSObjectType.get(sType);
    }
}
jonathanwiesel
  • 1,129
  • 14
  • 28

0 Answers0