Not sure how to really give an example that doesn’t feel contrived, so I instead put together a few steps that you can follow to pinpoint any bottle necks in your application. Also remember that with optimizations, there is going to be a cost-benefit relationship with time invested, so you should really decide whether shaving off a couple seconds is really necessary.
Steps:
1: Check out the browser’s network tab:
Open up the browser dev tools and inspect what is going on in the network tab. This tab allows you to inspect all the requests as they come in and breaks down their roundtrip time from request to response. Make sure that you are not in debug mode though so you don’t have see the constant interval of reloads which make it harder to focus on the requests that you trigger yourself.
Here is screenshot of my network tab when I toggle a dropdown in one of my applications. It took ~261ms to complete:
That said, I wouldn’t treat these as exact times since there are a lot of variables at play such as different hardware for starters. Rather, this should simply be used as a sniff test for any callbacks that are taking relatively longer than other ones, and thus warrant more review.
2.Profiling:
After you have have narrowed down your bottlenecks, you can now profile the underlying code, isolated from your dash application. Here are the python 3.x docs for the standard library profiler: https://docs.python.org/3/library/profile.html
3.Use timeit to compare alternative methods:
Once you have figured out the bottle neck, via profiling, you can now look into alternative implementations. You will want to test the contenders and see if they are an improvement or not. This can be done with python’s timeit module. Here are the relative docs for that one: https://docs.python.org/3.7/library/timeit.html. I don’t know why though, but I find it tricky to implement that module. I instead use the %timeit magic function in ipython/jupyter notebook as I don’t have to do any setup. Tell me that this isn’t as simple as it gets:
In [1]: %timeit [i*i for i in range(100)]
6.61 µs ± 79.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Hope this helps and good luck!