0

What is the best approach from below two for initializing an object instance while creation in a loop.

for(string str: stringList){ 
                        //create obj records 
                        object__c obj = new object__c();                        
                             obj.name = str.name;                  
                            InsertRecords.add(obj);   
                        }

second:

//create obj records 
                     object__c obj = new object__c();  
    for(string str: stringList){ 

            obj.name = str.name;                  
            InsertRecords.add(obj);   
       }
  • 4
    In your first example you are creating a new Object for each string in your stringList. In your second example you are only creating ONE object, adding it to the InsertRecords list for each iteration of your for loop, and ultimately that one object's name will be the last String in stringList. – dBeltowski Jun 22 '16 at 20:26
  • What's your use case? Your two examples will have 2 different outcomes – EricSSH Jun 22 '16 at 20:35
  • I don't have any scenario in specific. It should just create records from the loop. I have the first process in my code and works fine. I came across something that said declaration inside loops reduces the performance. So i wanted to know the difference between them and the best approach of doing this. – Anurag Jun 22 '16 at 20:38
  • Even most of the times i am going for 1st approach in test classes for bulk testing.@Anurag A.Even i want to know the best approach. – Pavan tej Jun 22 '16 at 20:50
4
//create obj records 
object__c obj = new object__c();  
for(string str: stringList){ 
    obj.name = str.name;                  
    InsertRecords.add(obj);   
}

This creates just one instance of a record, but will be in the list more than once. The result will be a "duplicate record in list" error when you try to insert it. Also, a noted in the comments, if you just try to use the records, they'll all mirror each other. This will also cause problems if you're trying to build a list for, say, editing in a Visualforce page, because the results will be unpredictable. In other words, just don't do this.


for(string str: stringList){ 
    //create obj records 
    object__c obj = new object__c();                        
    obj.name = str.name;                  
    InsertRecords.add(obj);   
}

This is a correct way to do this, because you're creating one instance per value. However, it's incredibly inefficient, and becomes increasingly so for each additional field you need to set.


Instead, skip the temporary entirely, and add it directly to the list:

for(String value: stringList) {
    insertRecords.add(new Object__c(Name=value));
}

Assigning a value to a field in the SObject constructor is at least twice as fast, which means that you'll gain more benefit the more fields you assign at once. Also, if you don't need the temporary variable, you'll gain something like a 50% speed boost compared to using the temporary.

| improve this answer | |
1

If you want to create a new object each time you loop through your stringList and have access it outside of your loop you could do something like this, but your use case isn't clear..

object__c obj;   
for(string str: stringList)
{ 
        obj = new object__c();  
        obj.name = str.name;                  
        InsertRecords.add(obj);   
}

Your two examples as Doug B said will give you 2 very different outcomes, what are you trying to do?

| improve this answer | |
  • I use this pattern as well. Declaring obj outside of the loop means that you aren't incurring the overhead associated with variable declaration on every iteration of the loop. The difference is small, but it becomes important when you start looping through thousands of records. – Derek F Jun 22 '16 at 21:53

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.