All Around Microsoft Dynamics Business Central
With newer versions of Business Central, it's now possible to have multiple page and table extensions for the same objects within a single app. This flexibility allows us to build apps in a modular way and group functionality together thematically.
However, this raises an interesting question: How should we handle event subscribers?
Traditionally, we often created one codeunit per functional area that subscribes to all events from that area in Business Central - for example, all sales-related events. The reasoning behind this was always:
"Subscribing to an event multiple times must be expensive!"
But is that really true? Or are we holding ourselves back from organizing our code more cleanly and modularly based on a false assumption?
To answer this question, I conducted a practical comparison test. The test scenario was simple:
A single event subscriber that calls an AddOne() function 100 times:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"PTE Process Subscription", OnFireOneEventSubscription, '', false, false)]
local procedure ProcessSubscription_OnFireOneEventSubscription(var i: Integer)
begin
AddOne(i); // Called 100 times
AddOne(i);
AddOne(i);
// ... total of 100 times
end;
One hundred separate codeunits, each subscribing to the same event and increasing the value by 1 only once:
codeunit 57040 "PTE Test ES 1"
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"PTE Process Subscription", OnFireOneHundredEventSubscription, '', false, false)]
local procedure ProcessSubscription_OnFireOneHundredEventSubscription(var i: Integer)
begin
AddOne(i); // Only once
end;
}
// ... 99 more identical codeunits
After 500 executions, the results were clear:
| Variant | Average Time |
|---|---|
| 1 Event Subscriber | 18 ms |
| 100 Event Subscribers | 72 ms |
Yes, 100 event subscribers are approximately 4x slower than a single subscriber. But let's look at this in context:
The performance cost is not high enough to justify writing messy code!
Instead of bundling all event subscribers into one "god codeunit," we should:
✅ Place event subscribers where they thematically belong
✅ Prioritize modularity and maintainability
✅ Readability over micro-optimization