Canvas Draw Image vs fill rect
JavaScript performance comparison
Info
Compares rendering to an offscreen buffer and drawing the resulting image versus drawing the image from scratch every frame.
Trying to figure out how big the impact is with different number of objects
Preparation code
<canvas id="myCanvas" width="600" height="400">
</canvas>
<script>
Benchmark.prototype.setup = function() {
canvas = document.getElementById('myCanvas');
ctx = canvas.getContext('2d');
var renderToCanvas = function(width, height, renderFunction) {
var buffer = document.createElement('canvas');
buffer.width = width;
buffer.height = height;
renderFunction(buffer.getContext('2d'));
return buffer;
};
var renderFunc = function(ctx) {
ctx.fillStyle = '#636363';
ctx.fillRect(110, 0, 10, 10);
ctx.fillRect(100, 10, 10, 10);
ctx.fillRect(90, 20, 10, 10);
ctx.fillRect(80, 30, 10, 10);
ctx.fillRect(70, 40, 10, 10);
ctx.fillRect(60, 50, 10, 10);
ctx.fillRect(50, 60, 10, 10);
ctx.fillRect(40, 70, 10, 10);
ctx.fillRect(30, 80, 10, 10);
ctx.fillRect(20, 90, 10, 10);
ctx.fillRect(10, 100, 10, 10);
ctx.fillRect(0, 110, 10, 10);
ctx.fillRect(220, 0, 10, 10);
ctx.fillRect(210, 10, 10, 10);
ctx.fillRect(200, 20, 10, 10);
ctx.fillRect(190, 30, 10, 10);
ctx.fillRect(180, 40, 10, 10);
ctx.fillRect(170, 50, 10, 10);
ctx.fillRect(160, 60, 10, 10);
ctx.fillRect(150, 70, 10, 10);
ctx.fillRect(140, 80, 10, 10);
ctx.fillRect(130, 90, 10, 10);
ctx.fillRect(120, 100, 10, 10);
};
var offScreenCanvas = renderToCanvas(canvas.width, canvas.height, renderFunc);
ctx.fillStyle = '#FF0000';
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Draw Image |
|
pending… |
Fill Rect |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 5: published by lump
- Revision 6: published
- Revision 8: published
- Revision 9: published by PlaneWorld
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published
- Revision 14: published
- Revision 15: published
- Revision 16: published
- Revision 17: published
- Revision 18: published
0 comments