I would like to measure the computing time of methods.
A nice way is (How do you performance test JavaScript code?) with console.time('Function #1');
and console.timeEnd('Function #1');
My idea is to add these console outputs on lifecycle-methods. In this case using SAPUI5 like createContent:funtion(){};
methods.
This should be possible with AOP using before()
and after()
to runt the time counting.
Which AOP framework would you suggest and how to implement it with the need of modifying the identification string "Function #1" automatically?
As it already got mentioned, one really is not in need of real Aspect-oriented Programming in order to solve such tasks in JavaScript. But this language might deserve some more standardized method-modifiers in addition to the already existing
bind
method.Please check back with my 2 most recent posts on this matter:
One does not need to since the console's
time
/timeEnd
functionality only has to have identical entry and exit points for measuring time (like the start/stop trigger of a stopwatch). So one gets along with exactly the reference of the function/method one is currently running/measuring.In order to solve the given task I will suggest
around
only instead of bothbefore
andafter
for the former generates less overhead. The next code block exemplarily shows a possible prototypal implementation. It also is the base for the afterwards following example that finally might solve the OP's task.The next example takes into account that method-modification essentially relies on functionality that is bound to an object. It is not just function wrapping. In order to not loose the context a method is operating on, context has to be delegated / passed around as
target
throughout all operations.For this the example does not modify
calculate
since it is not bound to an object but it modifiestrigger
instead.