Code Stage vs Loop stage: making Blue Prism collection work faster
Updated 25 September 2026 · Every C# sample here compiles with the compiler that ships with Blue Prism.
Most slow Blue Prism processes aren't slow because of the applications they drive. They're slow because they walk through a big collection on the canvas, one row and several stages at a time. This guide explains why that's slow, how to move the work into a single Code Stage, how to measure the difference in your environment, and when the canvas is still the better choice.
Why a canvas loop is slow
On the canvas, every stage is a separate step for the Blue Prism engine. For each stage it:
- evaluates the stage's expressions,
- reads and writes the data items involved,
- writes a session log entry, if logging is on for that stage,
- moves on to the next stage.
A typical "go through the rows" loop has a Loop stage, a Decision, and a Calculation or two inside it, so four or five stages per row. On a 50-row collection nobody notices. On tens of thousands of rows, the per-stage overhead is multiplied by every row, and the session log grows by one entry per stage per row.
A Code Stage is one step. The row work happens inside compiled .NET code, which doesn't write per-row logs.
The same job both ways
Here's a common one: total the Amount column for rows where Status is Open.
On the canvas: a Loop over Items, a Decision on [Items.Status] = "Open", and a Calculation [Total] + [Items.Amount] stored back into Total. That's three to four stages for every row.
In one Code Stage (input Items, output Total as a Number; add System.Globalization to Namespace Imports):
decimal total = 0m;
foreach (DataRow row in Items.Rows)
{
if (Convert.ToString(row["Status"]) == "Open" && row["Amount"] != DBNull.Value)
total += Convert.ToDecimal(row["Amount"], CultureInfo.InvariantCulture);
}
Total = total;
If Amount is a Blue Prism Number field (a decimal column), DataTable.Compute can do it in a single expression:
object sum = Items.Compute("SUM(Amount)", "Status = 'Open'");
Total = sum == DBNull.Value ? 0m : Convert.ToDecimal(sum);
Compute only works on a numeric column. On a Text column holding numbers, use the loop above.
Group totals
Totals per status, returned as a collection, would be a nested loop on the canvas. In code it's one pass:
var totals = new Dictionary<string, decimal>(StringComparer.OrdinalIgnoreCase);
foreach (DataRow row in Items.Rows)
{
string status = Convert.ToString(row["Status"]);
decimal amount = row["Amount"] == DBNull.Value ? 0m : Convert.ToDecimal(row["Amount"], CultureInfo.InvariantCulture);
decimal current;
totals.TryGetValue(status, out current);
totals[status] = current + amount;
}
DataTable result = new DataTable();
result.Columns.Add("Status", typeof(string));
result.Columns.Add("Total", typeof(decimal));
foreach (var pair in totals)
result.Rows.Add(pair.Key, pair.Value);
Totals = result;
Measure it yourself
Don't take anyone's benchmark for this, including ours. The gap depends on your Blue Prism version, logging settings, database and hardware. Measuring takes ten minutes:
- Build a test collection of 1,000 rows, then 10,000. A Code Stage can generate one quickly, or read it from a test Excel file.
- Add a Calculation
Now()into a DateTime data item before the work, and another after it. - Run the canvas version, then the Code Stage version, from Process Studio or Control Room with the same logging settings.
- Compare the two durations. Then compare how many session log rows each run wrote.
Running it at two sizes shows the trend. The canvas version grows with every row and every stage in the loop body, while the Code Stage version stays small.
If you keep a canvas loop
Sometimes the loop has to stay on the canvas, for example because each row drives an application. In that case:
- Turn down logging on the stages inside the loop. Each stage can log always, only on errors, or not at all. Stages inside a tight loop rarely need a log entry per row, and switching them off shrinks both runtime and the session log table.
- Move pure data work out of the loop. Filter first (without a loop), remove duplicates first, then loop only over the rows that need application work.
When the canvas is still the right choice
A Code Stage isn't free. Keep work on the canvas when:
- The logic is a business rule that analysts review. A Decision stage is readable by people who don't read C#.
- Your support team can't maintain code. A fast stage nobody can fix at 2 a.m. is a risk.
- The step drives an application. Spied elements, waits and navigation belong in Blue Prism's application modelling, not in a Code Stage.
A good rule: application work on the canvas, data work (filter, sort, dedupe, group, parse, format) in Code Stages.
Related
More Code Stage guides
- How to write a Code Stage in Blue Prism: inputs, outputs, namespaces and references
- Filter a Blue Prism collection without a loop (DataView and LINQ)
- Remove duplicate rows from a Blue Prism collection
- Parse JSON in a Blue Prism Code Stage (no third-party DLL)
- Blue Prism Code Stage compile errors and how to fix them
- Dates in Blue Prism Code Stages: parsing, formats and robot locale
- Regex in a Blue Prism Code Stage: match, extract and validate