Last active
November 25, 2024 16:26
-
-
Save ChuckJonas/8a7619b8204e3d1c534d017f9cb369d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String generateRandomString(Integer len) { | |
final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; | |
String randStr = ''; | |
while (randStr.length() < len) { | |
Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length()); | |
randStr += chars.substring(idx, idx + 1); | |
} | |
return randStr; | |
} | |
public static Account removePropertiesDoug(Account acct1, String propertyToRemove) { | |
Map<String, Object> acctMap1 = new Map<String, Object>(acct1.getPopulatedFieldsAsMap()); | |
// Remove the specified property | |
acctMap1.remove(propertyToRemove); | |
// Convert the map back to an sObject | |
Account acct2 = (Account) JSON.deserialize(JSON.serialize(acctMap1), Account.class); | |
return acct2; | |
} | |
public static List<Object> removePropertiesJonas(Type objectType, List<Object> objects, String[] propertiesToRemove) { | |
List<Object> deserializedList = (List<Object>) JSON.deserializeUntyped(JSON.serialize(objects)); | |
for (Object deserialized : deserializedList) { | |
Map<String, Object> obj = (Map<String, Object>) deserialized; | |
for (String property : propertiesToRemove) { | |
obj.remove(property); | |
} | |
} | |
return (List<Object>) JSON.deserialize(JSON.serialize(deserializedList), objectType); | |
} | |
// List sizes to test | |
Integer[] listSizes = new Integer[]{1, 10, 100, 1000, 2000}; | |
String[] propertiesToRemove = new String[]{'Website'}; | |
// Print header | |
System.debug('List Size | Jonas (ms) | Doug (ms)'); | |
for (Integer size : listSizes) { | |
// Generate a test list of Accounts | |
List<Account> testList = new List<Account>(); | |
for (Integer i = 0; i < size; i++) { | |
testList.add(new Account( | |
Name = 'Account' + i, | |
Website = generateRandomString(5 + Integer.valueOf(Math.random() * 25)), // Ensure a minimum string length | |
AnnualRevenue = Math.random() * 1000000, | |
NumberOfEmployees = Integer.ValueOf(Math.random() * 1000), | |
BillingStreet = generateRandomString(5 + Integer.valueOf(Math.random() * 25)), | |
BillingCity = generateRandomString(5 + Integer.valueOf(Math.random() * 25)) | |
)); | |
} | |
// Measure Method 1 | |
Long start1 = System.now().getTime(); | |
List<Object> cleanedList1 = removePropertiesJonas(List<Account>.class, testList, new String[]{'Website'}); | |
Long elapsed1 = System.now().getTime() - start1; | |
// Measure Method 2 | |
Long start2 = System.now().getTime(); | |
List<Account> cleanedList2 = new List<Account>(); | |
for (Account acct : testList) { | |
cleanedList2.add(removePropertiesDoug(acct, 'Website')); | |
} | |
Long elapsed2 = System.now().getTime() - start2; | |
// Output results | |
System.debug(size + ' | ' + elapsed1 + ' ms | ' + elapsed2 + ' ms'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<Object> removePropertiesDougBulkified( Type objectType, SObject[] sObjects, String[] propertiesToRemove){ | |
List<Map<String, Object>> objFieldMapList = new List<Map<String, Object>>(); | |
for(SObject sob : sObjects){ | |
Map<String, Object> objFieldMap = new Map<String, Object>(sob.getPopulatedFieldsAsMap()); | |
// Remove the specified property | |
for (String property : propertiesToRemove) { | |
objFieldMap.remove(property); | |
} | |
objFieldMapList.add(objFieldMap); | |
} | |
// Convert the map back to an sObject | |
return (List<Object>) JSON.deserialize(JSON.serialize(objFieldMapList), objectType); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List Size | Jonas (ms) | Doug (ms) | |
1 | 2 ms | 0 ms | |
10 | 5 ms | 5 ms | |
100 | 46 ms | 58 ms | |
1000 | 333 ms | 463 ms | |
2000 | 637 ms | 767 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment