Cover Image for Before + After Plugin VS Around Plugin

Before + After Plugin VS Around Plugin


There is no difference between the two implementations

I recently encountered a situation in which I needed to do something before AND after a method was run in Magento. Two options present themselves:

  1. Create a before AND after plugin on the method
  2. Create a single around plugin on the method

There is no difference in the result of the logic so the decision would be made on which is more performant.

To test a created an Observer on controller_action_postdispatch. The Observer has an empty execute method.

I then created a before plugin which starts a timer and an after plugin which stops the timer. The elapsed time is written to the logs.

The result was:

BeforeAfter | Start: 1666847559.454829, Stop: 1666847559.454833, Difference: 0.000004

I disabled those plugins and created a single around plugin for the same method. The timer is started at the begining of the method, then proceed is called, then the timer is stopped at the end of the method. The elapsed tiem is written to the logs.

The result was:

Around | Start: 1666847619.240797, Stop: 1666847619.240801, Difference: 0.000004

No difference.

Source code in case you want to try it yourself: PluginTest.rar