var img = new Image();
var imageBitmap;
var doneLoading = false;
var canvasImage = document.createElement('canvas');
canvasImage.width = 700;
canvasImage.height = 700;
var canvasImageBitmap = document.createElement('canvas');
canvasImageBitmap.width = 700;
canvasImageBitmap.height = 700;
var canvas = document.createElement('canvas');
canvas.width = 700;
canvas.height = 700;
var canvasCanvas = document.createElement('canvas');
canvasCanvas.width = 700;
canvasCanvas.height = 700;
var ctxImage = canvasImage.getContext('2d', { alpha: false });
var ctxImageBitmap = canvasImageBitmap.getContext('2d', { alpha: false });
var ctx = canvas.getContext('2d', { alpha: false });
var ctxCanvas = canvasCanvas.getContext('2d', { alpha: false });
async function globalMeasureThatScriptPrepareFunction() {
return new Promise((resolve, reject) => {
img.addEventListener('load', function() {
createImageBitmap(img)
.then(function(bitmap) {
imageBitmap = bitmap;
debugger;
doneLoading = true;
console.log("bitmap preparation done");
resolve(true);
}, false);
ctx.drawImage(img, 0, 0);
console.log("canvas preparation done");
});
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg';
});
}
ctxImage.drawImage(img,0,0);
ctxImage.clearRect(0,0,700,700);
ctxImageBitmap.drawImage(imageBitmap,0,0);
ctxImageBitmap.clearRect(0,0,700,700);
ctxCanvas.drawImage(canvas,0,0);
ctxCanvas.clearRect(0,0,700,700);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
ImageBitmap | |
Canvas |
Test name | Executions per second |
---|---|
Image | 959459.8 Ops/sec |
ImageBitmap | 836179.8 Ops/sec |
Canvas | 898583.6 Ops/sec |
The benchmark titled "Canvas context2d: Image vs ImageBitmap vs Canvas (with clear canvas)" is designed to evaluate the performance of different approaches to drawing images in an HTML <canvas>
element using the 2D rendering context. Specifically, it tests three methods for rendering: using a standard image (Image
), an ImageBitmap
, and another canvas (Canvas
). Each of these methods has its own strengths and weaknesses, which will be covered below.
Image (ctxImage.drawImage(img, 0, 0);
):
Image
object to draw an image onto the canvas.ImageBitmap (ctxImageBitmap.drawImage(imageBitmap, 0, 0);
):
ImageBitmap
, a more efficient representation of image data optimized for drawing.createImageBitmap()
function converts an Image
into an ImageBitmap
, allowing for quicker rendering.ImageBitmap
which involves an asynchronous operation.Canvas (ctxCanvas.drawImage(canvas, 0, 0);
):
ImageBitmap
.Performance: The benchmark results show that the method using the Image
approach results in the highest average executions per second (959,459.75), followed closely by the Canvas
approach (898,583.5625), and finally the ImageBitmap
approach at (836,179.75). This indicates that while the ImageBitmap
method is efficient for certain use cases, it may not always be the fastest for simple static images.
Use Cases: Choosing the appropriate method does depend on the specific scenario:
Image
may still be the best option.ImageBitmap
might provide better performance.In addition to these methods, alternative approaches include:
In summary, this benchmark highlights important performance considerations when using different methods for rendering images in <canvas>
elements, providing insights into which method may be best suited depending on the requirements of the application.