Currently, I am in the process of developing a test platform dedicated to JavaScript performance competitions. Among the various challenges presented to participants, one task involves optimizing JavaScript code that handles a canvas animation. Once a solution is submitted, the server executes it using PhantomJS and determines the average frames per second (FPS) after 20 seconds of animation. However, I am encountering an issue where both optimized and unoptimized code are yielding only 3-4FPS, making it difficult to gauge the effectiveness of optimization efforts.
Here are some key points to consider:
- I have confirmed that PhantomJS accurately renders the animation (verified through screenshots).
- In a browser, unoptimized code achieves 13FPS while optimized code performs at 58FPS.
- Due to PhantomJS not supporting requestAnimationFrame, I had to implement a polyfill as a workaround.
- For FPS testing, I am utilizing the code snippet provided below:
frameCounter.js
var frameCounter = (function() {
var frames = 0;
var startTime = new Date();
function bump() {
frames++;
window.requestAnimationFrame(bump);
}
bump();
return {
getFPS: function() {
var time = (new Date() - startTime) / 1000;
return (frames/time).toPrecision(4);
}
}
})();
My primary query revolves around finding a reliable method to programmatically measure the performance of a canvas animation.