Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Canvas clearing performance v3
https://stackoverflow.com/questions/2142535/how-to-clear-the-canvas-for-redrawing
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0
Browser:
Firefox 136
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Clear only
53382.7 Ops/sec
Save transform Clear restore
51777.2 Ops/sec
Canvas w with memo
5644.2 Ops/sec
w=w
1893.6 Ops/sec
w+=0
2937.4 Ops/sec
fillRect
2990.7 Ops/sec
createImageData
495.9 Ops/sec
HTML Preparation code:
<canvas id="canvasOne" width="1600" height="800"></canvas> <canvas id="canvasTwo" width="1600" height="800"></canvas> <canvas id="canvasThree" width="1600" height="800"></canvas> <canvas id="canvasFour" width="1600" height="800"></canvas>
Script Preparation code:
var canvasOne = document.getElementById("canvasOne"); var ctx = canvasOne.getContext("2d", {desynchronized: true, alpha: true, preserveDrawingBuffer: true}); var canvasTwo = document.getElementById("canvasTwo"); var ctxTwo = canvasTwo.getContext("2d"); var canvasThree = document.getElementById("canvasThree"); var ctxThree = canvasThree.getContext("2d"); var canvasFour = document.getElementById("canvasFour"); var ctxFour = canvasFour.getContext("2d");
Tests:
Clear only
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height); ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();
Save transform Clear restore
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); ctx.save(); ctx.setTransform(1,0,0,1,0,0); ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height); ctx.restore(); ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();
Canvas w with memo
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); var w = ctx.canvas.width; ctx.canvas.width = 1; ctx.canvas.width = w; ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();
w=w
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); ctx.canvas.width = ctx.canvas.width; ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();
w+=0
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); ctx.canvas.width += 0; ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();
fillRect
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); ctx.fillStyle = 'rgba(0, 0, 0, 1)'; ctx.fillRect(0,0,ctx.canvas.width, ctx.canvas.height); ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();
createImageData
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'; ctx.fillRect(0, 0, 400, 400); var i = ctx.createImageData(ctx.canvas.width, ctx.canvas.height); ctx.putImageData(i, 0, 0); // clear context by putting empty image data ctx.beginPath(); ctx.fillStyle = 'rgba(231, 76, 60, 0.1)'; ctx.arc(200, 200, 100, 0, Math.PI * 2, true); ctx.fill();