14

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?

22

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
  • 6
    this is good news as the get() takes 4 more chars to type :-) – cropredy Mar 1 '16 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 '16 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 '16 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 '16 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 '16 at 23:05
3

I don't know if Salesforce changed its algorithms, but I just tested it, both in my Sandbox and Production, and it turns out the method List.get() is almost always faster than square brackets [i].

My first attempt, running List.get() prior to running square bracket:

// Square Brackets vs List.get()
   List<Integer> numbers = new List<Integer>();
   for (Integer i = 0; i < 1000000; i++) numbers.add(i);

   Integer x;

// List.get()
   Long startBracket = Datetime.now().getTime();
   for (Integer i = 0; i < 1000000; i++) x = numbers.get(i);
   System.debug(Datetime.now().getTime() - startBracket); // 1422

// Square Brackets
   Long startGet = Datetime.now().getTime();
   for (Integer i = 0; i < 1000000; i++) x = numbers[i];
   System.debug(Datetime.now().getTime() - startGet); // 1528

Then I ran it again. The results:

  • get: 1575
  • brackets: 1859

Maybe the problem was the execution order? Well, I've inverted it, just in case:

// Square Brackets vs List.get()
   List<Integer> numbers = new List<Integer>();
   for (Integer i = 0; i < 1000000; i++) numbers.add(i);

   Integer x;

// Square Brackets
   Long startBracket = Datetime.now().getTime();
   for (Integer i = 0; i < 1000000; i++) x = numbers[i];
   System.debug(Datetime.now().getTime() - startBracket); // 1526

// List.get()
   Long startGet = Datetime.now().getTime();
   for (Integer i = 0; i < 1000000; i++) x = numbers.get(i);
   System.debug(Datetime.now().getTime() - startGet); // 1352

I've repeated this test several times, in both Sandbox and Production, with similar results.

Could you guys please repeat the test on your end just in case? Because it seems now it's faster to use List.get().

  • I am seeing 1279/2780 and 980/2998 - So I am seeing 2-3 times slower got .get in a DE org – Eric Apr 17 '17 at 13:31
  • Just to make sure, which one is slower on your end, Eric? – Matheus Goncalves Apr 17 '17 at 14:06
  • .get............. – Eric Apr 17 '17 at 15:57
  • 2
    Just tested it in a Playground (Trailhead) and the List.get(i) method was indeed slower than [i] on every test. Now I wonder why it differs so much from server to server. – Matheus Goncalves Apr 17 '17 at 16:05
  • 1
    Yes, there does seem to be some volatility, but I haven't found a definitive answer to why, nor any sort of pattern, so I haven't updated my post. – Adrian Larson May 5 '17 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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