Canvas Draw Image vs fill rect

JavaScript performance comparison

Revision 23 of this test case created

Preparation code

<canvas id="myCanvas" width="600" height="400"></canvas>
<script>
Benchmark.prototype.setup = function() {
    var canvas = document.getElementById('myCanvas');
    var ctx = canvas.getContext('2d');
   
    var m_canvas = document.createElement('canvas');
            m_canvas.width = 4;
            m_canvas.height = 4;                                
            var m_context = m_canvas.getContext('2d');
   
    m_context.fillStyle = 'rgb(255, 0, 0)';
    m_context.beginPath ();
    m_context.fillRect (0, 0, 4, 4);
   
    var imageData = ctx.createImageData(4, 4);
    for ( var index = 0; index < 4*4; index += 4 )
    {
        imageData.data[index+0] = 123;
        imageData.data[index+1] = 123;
        imageData.data[index+2] = 123;
        imageData.data[index+3] = 255;
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Draw Image
ctx.drawImage(m_canvas, Math.random () * 100, Math.random () * 100, 4, 4);
pending…
Fill Rect
ctx.fillStyle = '#ccc';
ctx.fillRect (Math.random () * 100, Math.random () * 100, 4, 4);
pending…
putImageData
ctx.putImageData(imageData, Math.random () * 100, Math.random () * 100);
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:

0 comments

Add a comment