I am creating a small Java web application, where I have defined a routes file, which specifies for each action the method of the controller that should be executed.
In order to do this, I parse this routes file, and in my main Servlet, based on the received call, I use reflection in order to invoke the required method.
However, Java reflection causes a performance overhead (based on the JVM, etc).
Thus I would like to ask, if this is the right procedure, or there is another alternative to perform such task.
Thanks a lot in advance!
The performance overhead of invoking the method by reflection is negligible in this case: I estimate that a reflective method call will take on the order of 0.1 microseconds, while the network latency between client and server is easily 10 milliseconds. That is, less than 0.001% of response time is due to reflection overhead.
Take home message: When being told that something is “slow”, always ask “compared to what”, and try to assess whether it could conceivably have a measurable performance impact before optimizing it for performance. (And if you have an actual performance problem to solve, measure which parts of your program are slow, optimize those – and then measure again).