Pranay
Pranay

Reputation: 11

Can looping over a for() loop of low size take less time to execute? Apex/Salesforce

Say I have two SOQL queries. I think the same can be answered by a Java or any other language guy.

Account[] a1= [select id from Account]; 
Account[] a2= [select id, name, etc.etc from Account];

I get that the time to return the result set would differ but would looping over them take the same time.

like this,

for(Account a : a1){} 
for(Account a : a2){} 

Upvotes: 0

Views: 120

Answers (1)

Eran
Eran

Reputation: 394146

If I understand the question, you are asking whether iterating over a list whose elements have "less" initialized properties takes less time.

First of all, at least in Java, it doesn't matter if Account has 2 properties or 200 properties. The for loop is iterating over Account references, so it doesn't read the actual properties stored in each instance (unless you access these properties in the body of the loop).

Second of all, even if the time to iterate over the elements depended on the size of each Account instance, it wouldn't matter if you only assign value to the ID property of the Account or assign values to other properties as well. The size of each Account instance would still be the same, since this size is determined by the size of all the properties of the Account class (both primitive and reference types), and they take the same space regardless of whether you initialize them or leave them with their default values.

Upvotes: 2

Related Questions