Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know which approach is better in Salesforce when getting element from list.

Is this faster?

myList.get(0);

Or is this faster?

myList[0];

Using large list which above statement is more efficient?

share|improve this question
    
Is this still an open question? I would add more detail to my answer, but I am not sure what else you could be looking for. – Adrian Larson Mar 12 at 2:01
    
Hey @AdrianLarson, thank you for the answer. It is well written concise answer. I am happy to know that I didn't used or recommend using .get(in);. even I didn't knew its performance drawback. I never wanted to write 5 letters more! – Ashwani Mar 14 at 9:33
up vote 11 down vote accepted

The following methodology may be somewhat faulty but demonstrates a large difference. The get approach is more than 4 times slower.

Square Brackets

List<Integer> numbers = new List<Integer>();
for (Integer i = 0; i < 1000000; i++) numbers.add(i);

Integer x;
Long start = Datetime.now().getTime();
for (Integer i = 0; i < 1000000; i++) x = numbers[i];
system.debug(Datetime.now().getTime() - start); // 1233

Get Method

List<Integer> numbers = new List<Integer>();
for (Integer i = 0; i < 1000000; i++) numbers.add(i);

Integer x;
Long start = Datetime.now().getTime();
for (Integer i = 0; i < 1000000; i++) x = numbers.get(i);
system.debug(Datetime.now().getTime() - start); // 5065
share|improve this answer
4  
this is good news as the get() takes 4 more chars to type :-) – cropredy Mar 1 at 20:58
    
Interesting. A related article salesforce.stackexchange.com/questions/5447/… Maybe try using a variable to capture the time so the debug statement doesn't alter the time. – Chris Fellows Mar 1 at 20:59
    
@ChrisFellows There is only one debug statement at the end. There's no way it adds more than 1-2ms. Actually if you remove the loop between the two statements you get 0. – Adrian Larson Mar 1 at 21:01
1  
Running Adrian's code with a separate stop variable in execute anonymous my results were 1923/1670/1539/1347 and 7679/9810/8102/8310 so consistent with the "more than 4 times slower". Surprisingly big. And I'm with @crop1645 on the less characters to type (and clearer resulting code). – Keith C Mar 1 at 21:52
3  
@ChrisFellows I'm going to disagree with that statement. A wash is when all things are equal. Using a method that's 5x slower isn't a wash when it happens all over the place. You can easily end up adding 1-2 seconds to a transaction in just a handful of loops. I've seen and written code that would have been crippled by this seemingly insignificant amount of time. – sfdcfox Mar 1 at 23:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.