HTML5 Canvas performance: low-level optimization tips and tricks (elekslabs.com)
Low-level optimizations matter even if you write your code in high-level language such as JavaScript. Code is still executed on same hardware as if you write it on C++.
Each JavaScript engine is different. You can have significant performance boost in one browser, but at the same time your fix may make your code slower in another browser. Test your code in all browsers your application must work with.
Keep the balance between code readability and performance considerations. Sometimes it makes sense to inline function even if it makes your code less readable. But always make sure that it brings you desired value: it doesn’t make sense to sacrifice code readability for 2 ms performance boost for code that is already fast enough.
Think about object allocation, memory usage and cache utilization, especially if you work with memory intensive algorithms.
5 comments
[ 4.0 ms ] story [ 26.1 ms ] threadIf you are interested, I have a decently quick floodfill here. Might be worth a benchmark but never got around to it: https://gist.github.com/4071852
Another thing is, this stuff can easily be thrown into a webworker, which will definitely help with some more complicated fills on larger canvases.
EDIT: If you're micro-optimizing, why not cache '((hitColor >> 24) & 0xFF)' in a local at the top of the function instead of computing it every iteration? Loop invariant code motion might hoist it for you, but I don't know why you wouldn't do that by hand.
Also, wrapping your integer arithmetic in ( ) | 0 may improve its performance in Firefox and some other browsers because it allows the JIT to omit overflow checks. Should help since you're doing a lot of addition to index into arrays.
my nightly FF does in 500ms what FF16 does in 250ms and chrome does in 200ms :(