You need to sign in to do that
Don't have an account?
AmWe
Apex For Loop
Hi All,
Type 1 : for(Account ac : [SELECT Id FROM Account LIMIT 100])
Type 2 :List<Account> acs = [SELECT Id FROM Account LIMIT 100];
for(Account ac : acs)
Type 3 :List<Account> acs = [SELECT Id FROM Account LIMIT 100];
for(Integer i=0,j=acs.size();i<j;i++)
From above 3 looping methods we identified that type 3 is bit more faster than type 2 and Type 1 is very much slower compared to 2 and 3.
But type 1 consumes very little heap size compared to 2 and 3.
What is the reccomended method to use? Salesforce recormends to use type 1 since it consumes less heap. if we didn't use that, will it be considered in salesforce code reviews
Type 1 : for(Account ac : [SELECT Id FROM Account LIMIT 100])
Type 2 :List<Account> acs = [SELECT Id FROM Account LIMIT 100];
for(Account ac : acs)
Type 3 :List<Account> acs = [SELECT Id FROM Account LIMIT 100];
for(Integer i=0,j=acs.size();i<j;i++)
From above 3 looping methods we identified that type 3 is bit more faster than type 2 and Type 1 is very much slower compared to 2 and 3.
But type 1 consumes very little heap size compared to 2 and 3.
What is the reccomended method to use? Salesforce recormends to use type 1 since it consumes less heap. if we didn't use that, will it be considered in salesforce code reviews
Chandra Sekhar CH N V
When you use Type1 salesforce processes the records returned in the list in chunks (more or less like a batch ) hence it is considered as a recommended one.