Measuring performance of 2 Apex ways of populating a map from a list of sObjects

PHOTO EMBED

Sat Jul 20 2024 12:59:11 GMT+0000 (Coordinated Universal Time)

Saved by @taurenhunter

List<Account> accountList = [
    SELECT Id, Name, AccountNumber FROM Account WHERE Name LIKE 'Test Account - %' 
    LIMIT 10
];

Datetime startDttm = System.now();
Map<String, SObject> acctMapByNbr = 
    MapCreator.createMapByIndex( accountList, 'AccountNumber' );
system.debug( 'MapCreator -> ' + ( System.now().getTime() - startDttm.getTime() ) );

startDttm = System.now();
Map<String, SObject> acctMapByNbr2 = 
    new Map<String, SObject>();
for( Account anAccount : accountList ) {
	acctMapByNbr2.put( String.valueOf( anAccount.AccountNumber ), anAccount );
}
system.debug( 'standard map creation -> ' + ( System.now().getTime() - startDttm.getTime() ) );
content_copyCOPY