Blog Post

Azure Integration Services Blog
3 MIN READ

Using Inline Code instead of a Foreach Loop for better performance in Logic Apps

Omar_Abu_Arisheh's avatar
May 17, 2022

Are you not getting the performance levels that you want when using Foreach Loops in Logic Apps?

 

The foreach loop with variables is slow by design, because each time the array variable is updated there is a round trip to storage and there is contention as well. “Foreach” scope is intended for running scenarios that require durable pan-out and pan-in and not intended for data transformation.

 

The foreach loop is not intended to be used for data transformation or heavy loads. Assigning variables in foreach loops can become a heavy scenario if the iterations are high, this is because the foreach persists the data information in storage and the round trip adds up to the total time of the process. A better solution is to use inline code for this purpose which will give you much better results.

 

Before we start with our simple solution, let's explore some other options that we have to accomplish the same:

 

 

Now the inline script solution:

 

Instead of using Append to array variable, you can remove the loop and replace it with inline code.

Reference: Add and run code snippets by using inline code - Azure Logic Apps | Microsoft Docs

 

Using inline code in Logic Apps Consumption requires an Integration Account, while in Logic App Standard it doesn't.

Single-tenant versus multi-tenant Azure Logic Apps - Azure Logic Apps | Microsoft Docs

 

The script below, very simple, and you can alter it based on your scenario, like returning CSV, concatenate data, or parse JSON in different ways, etc.

 

I am using body.body because my testing JSON has it twice. In your scenario reference your JSON data as it is.

 

var myinput = workflowContext.actions.Parse_JSON.outputs.body.body;

var myarray = [];

for(var i=0;i<myinput.Table1.length;i++)

{

myarray.push(myinput.Table1[i].Office);

}

return myarray;

 

 

 

Please Note: That the size limit that the script can handle is max 50MB, and can only run for 5 seconds for Consumption, and 15 seconds for Standard. Limits and configuration reference guide - Azure Logic Apps | Microsoft Docs

You can use/reference Node.js functions in inline code as well. Add and run code snippets by using inline code - Azure Logic Apps | Microsoft Docs

 

If your scenario requires more running time for the script, you can move the inline code to a function app and use it.

 

Updated May 13, 2022
Version 1.0
No CommentsBe the first to comment