Build Different with Power Automate – Episode 1: Mystery of Apply to each

In recent times, there has been a lot of discussions and arguments about pro developers versus citizen developers. With Power Automate (Microsoft Flow), even kids can start playing around and build something substantial. Power Automate is that good! Like every other tool, we need to use it properly and not abuse it. Without proper guidance and careful thought, it can become a nightmare as with pro development.

In this post, we are going to explore what can go wrong with pro developers while using Flow in Power Automate, and how to get the best out of Flow.

The puzzle

A user will enter a sentence. The program contains a pre-defined list of words (150 words). The task is to find out

  1. if the sentence contains any words from the pre-defined list and
  2. what those words are

Pretty easy, right?

A pro developer using a C# program

I can easily write the following program to go over every word in a for loop and use IndexOf to match words.

C# Program – For loop with IndexOf

This program runs blazing fast and takes less than 1 millisecond.

The result from the For loop with IndexOf

Convert it into Flow in Power Automate

Can we create a Flow in Power Automate that does the exact same thing? Of course, we can. You can see there are only two initialisations of variables, one Apply to each and a condition inside the Apply to each.

Loop in Microsoft Flow

Although I don’t expect the Flow to run as fast as the C# program, I expect it to be completed in under 20 seconds. But, the results are unbelievably slow.

It constantly takes more than 30 seconds. In this screenshot, it took 38 seconds to loop 150 items.

The result of Flow Loop

Why does it take such a long time to loop 150 records?

Parallelism

When you add an action ‘Apply to each’ action in the Flow, it runs 1 loop at a time. That means its parallelism is set to 1 by default. What if we turn on concurrency control and set its parallelism to 20, from Settings of the loop action?

Concurrency control and degree of Parallelism

The improvement here is unbelievable are surprising again. It took only 4 seconds. From 38 seconds to 4 seconds, it is about a 90% performance gain. Of course, there are many caveats around turning concurrency control on. We that we will discuss them upcoming post.

The result of Flow with 20 degree of Parallelism

A question

Can we still make an improvement? Can we go for an extra mile?

Stay tuned for the next post.