Since I have wondered about this issue myself and there were no direct responses to this aspect, I have looked into discovering lagginess before it happens.
The seminal work - one of the few I know about - is Don Olmstead's presentation at the 2013 HTML 5 conference in San Francisco titled
Optimizing WebGL Animations.
I have tried to penetrate the depth of observations that Don offers more than once, but after some hours my tiny brains fails me. If nothing else there is a whole learning curve regarding successful use of the JavaScript console that I have never completed.
In any case, what I am looking for is something that just pops out a simple number telling me that my script is running slowly. This should be part of the script and have no need to access the console. A search of the web offered no direct response, thus I was forced to do some more investigating myself.
In matters of this nature, the first shoulders I look to stand on are, of course: Mr.doob's. His
Stats.js is frequently referenced - even by non-Three.js scripts.
Typically the little Stats.js box that displays on a corner of your screen displays the frames per second that your script is displaying. Clicking in the box cause the milliseconds between frames to display. Clicking again display the memory being used. With luck your script is displaying at 60 fps - the maximum allowed - and thua taking about 16 ms per frame.
These measurements are great but if your frame rate drops below 60 then it is *already too late*. What is needed is an indication that the script is heading in the direction of dropping below 60 fps well before this event occurs.
So I started thinking about writing such a mechanism, but, of course, Mr.doob has taken care of that already. Stats.js has two calls to help: stats.begin() and stats.end() used this way:
var update = function () {
stats.begin();
// monitored code goes here
stats.end();
requestAnimationFrame( update );
};
requestAnimationFrame( update );
This display tells you how many milliseconds your script takes to process the frame. Actually I seem to like the measurement done this way:
var update = function () {
stats.end();
// monitored code goes here
stats.begin();
requestAnimationFrame( update );
};
requestAnimationFrame( update );
This display tells you how many milliseconds is available to other apps and processes. When this number drops down to low single digits, your script - and mine now - better start reducing its use of resources.
Here is a little 2D, no Three.js demo I built that shows both measurements in one script:
Note: this revision of the script does not attempt to self correct for over use of resources, but I do hope to cover this featurein a future release.
So, Philip, thank you for posing the issue - and causing me to look for solutions to my issues. Catching lost frames and that sort of stuff is way beyond my pay grade, but I hope these simple observations will help you somewhat to wrangle your errant milliseconds.
Theo