I spent some time this week trying to fix the speed issues and layout issues that I have been seeing. The major speed bottleneck in the initial page load was the call to fetch all of the documents. It took a few seconds to fetch a few thousand results. Things I tried:
- I trimmed this call down to send back only the data that I needed. This reduced the time by a few tenths of a second.
- Next I tried batching the call so I would only fetch 50 records at a time, this resulted in an overall higher response time, but I could start rendering right away. Each batch still took 500ms on average after the initial call. This gives an average of 10ms per record which is still very slow.
- I ditched JPA and went straight to the database which resulted in 10ms response times for each batch of 50 records. This was much better.
- After removing JPA, I decided I would try something completely different. I spent a couple of hours rebuilding the fetch documents API call in NodeJS. This resulted in 5ms response times per batch of 50 records.
I honestly didn't expect JavaEE to be so slow. I suppose it makes sense since you have the JPA layer between your code and the database. In all likelihood I could improve the performance of the JPA-approach to bring it close to the 10ms response time I saw with the third approach. However, I don't think it makes sense to spend time figuring that out when the NodeJS solution is not only faster, but cleaner.
Unfortunately, this means that I will end up starting over on the server code.
I've always heard "optimize last". I've interpreted it as something to do at the end of your project, but I believe it is meant to do at the end of your feature. You should be looking at your numbers during the course of development as it could completely change the libraries and platform you are using. I wouldn't care about squeezing every millisecond out of your response times, but for sure if a call is taking hundreds of milliseconds or more, I would dig into it sooner otherwise you could be wasting precious days or weeks rewriting your code.