var img = new Image();
var imageBitmap;
var doneLoading = false;
var canvasImage = document.createElement('canvas');
canvasImage.width = 640;
canvasImage.height = 480;
var canvasImageBitmap = document.createElement('canvas');
canvasImageBitmap.width = 640;
canvasImageBitmap.height = 480;
var canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
var canvasCanvas = document.createElement('canvas');
canvasCanvas.width = 640;
canvasCanvas.height = 480;
var ctxImage = canvasImage.getContext('2d');
var ctxImageBitmap = canvasImageBitmap.getContext('2d');
var ctx = canvas.getContext('2d');
var ctxCanvas = canvasCanvas.getContext('2d');
async function globalMeasureThatScriptPrepareFunction() {
return new Promise((resolve, reject) => {
img.addEventListener('load', function() {
Promise.all([
createImageBitmap(img)
]).then(function(images) {
imageBitmap = images[0];
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);
ctxImageBitmap.drawImage(imageBitmap,0,0);
ctxCanvas.drawImage(canvas,0,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
ImageBitmap | |
Canvas |
Test name | Executions per second |
---|---|
Image | 1272113.4 Ops/sec |
ImageBitmap | 93714.0 Ops/sec |
Canvas | 89946.4 Ops/sec |
The benchmark titled "Canvas context2d: Image vs ImageBitmap vs Canvas (new)" compares the performance of three different methods for rendering images in an HTML canvas context using JavaScript. Specifically, it assesses the following three approaches:
Image: This method uses a traditional HTML <img>
element to load an image and then renders it onto a canvas using the drawImage
method of the 2D rendering context.
ctxImage.drawImage(img, 0, 0);
Pros:
<img>
element is well-documented and has a straightforward loading mechanism.Cons:
ImageBitmap: This approach involves creating an ImageBitmap
object, which is a more efficient representation of an image that can be used in rendering operations. It utilizes the createImageBitmap
function to create the bitmap from the loaded image.
ctxImageBitmap.drawImage(imageBitmap, 0, 0);
Pros:
ImageBitmap
instances are optimized for rendering and can provide better performance in contexts where images are redrawn frequently.Cons:
createImageBitmap
function may not be supported in older browsers.Canvas: In this method, another canvas element is drawn onto the target canvas. The image is first rendered to an offscreen canvas before copying to the main one.
ctxCanvas.drawImage(canvas, 0, 0);
Pros:
Cons:
The benchmark results show the number of executions per second for each method as follows:
From these results, we can infer that using the traditional <img>
element is significantly more performant in this test case compared to the other two approaches. The ImageBitmap
and Canvas
methods yield comparatively lower execution rates, which may suggest additional overhead in their respective approaches.
<img>
element may be suitable for most applications, while ImageBitmap
can be beneficial for scenarios demanding high-performance rendering, such as games or graphical applications.Besides the mentioned methods, developers may also consider: