I've done a lot of experiments with canvas in many browsers. Some performance issues that I noticed:
First, about your guessing:
When
requestAnimationFrame
is supported by a browser, the drawing stuff and the app itself are more responsive. UsesetTimeout
orsetInterval
as a fallback are always possible but you need to be careful about the timing. This robust polyfill may help a little, but nothing compared to native requestAnimationFrame.If console.log is called every frame (or almost), yes the performance goes down. Since native Android Browser doesn't have a console Object, every time it is called will generate an error that also contributes to slow down your application. You can do this:
if(typeof console === "undefined"){ console = {}; }
For intense real-time applications web sockets are faster then http requests. Unfortunately this feature is not supported by old native android browsers. If it's not possible to use web sockets, you should minimize the http requests.
Note: Chrome for android support most of HTML5 features cited here, including requestAnimationFrame
and websockets
.
More information:
Drawing text using the context 2d
fillText
it's too expensive, but in some browsers this is even worse. Pre-render your texts in another canvas or use bitmap fonts. (In native Android Browser, after replacefilltext
drawing for pre-render stuff, the performance grew from 10-15 FPS to 30-45 FPS in some games I've made).Avoid scale and rotate context because they also cause drop down in performance. If you need to scale or rotate a sprite only once, use pre-render either.
You need to minimize the real time drawing. Pre-render your stuff whenever you can. Redraw only stuff that changed and needs to be updated.
Try to write memory efficient and garbage collector friendly code.
There are a lot more things to do. I just cited a few.
TIP: make some stress tests for features you don't sure if they are performance killers and capture the benchmark results.
In mobile applications, specially real time apps, all optimizations are welcome no mater if it's just an over optimization or a bit of memory gain.
For more information follow the links below:
- http://www.html5rocks.com/en/tutorials/canvas/performance/ (this one definitely should be visited)
- http://www.html5rocks.com/en/features/performance
Also search for performance in Posts & Tutorials.
EDIT
This jsfiddle code snippet shows some stuff covered in this answer and provides a rough fps counter to benchmark. Edit this fiddle by yourself and check it out.