var img = new Image();
var imageBitmap;
var doneLoading = false;
let canvasImage = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
let canvasImageBitmap = document.createElement('canvas');
canvas2.width = 640;
canvas2.height = 480;
let canvas = document.createElement('canvas');
canvas2.width = 640;
canvas2.height = 480;
let canvasCanvas = document.createElement('canvas');
canvas2.width = 640;
canvas2.height = 480;
var ctxImage = canvasImage.getContext('2d');
var ctxcImageBitmap = canvasImageBitmap.getContext('2d');
var ctx = canvas.getContext('2d');
var ctxCanvas = canvasCanvas.getContext('2d');
img.addEventListener('load', function() {
Promise.all([
createImageBitmap(img)
]).then(function(images) {
imageBitmap = images[0];
doneLoading = true;
}, false);
});
if (doneLoading) {
ctx.drawImage(img,0,0)
}
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg';
if (doneLoading)
ctxImage.drawImage(img,0,0);
if (doneLoading)
ctxImageBitmap.drawImage(imageBitmap,0,0);
if (doneLoading) {
ctxCanvas.drawImage(canvas,0,0)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
ImageBitmap | |
Canvas |
Test name | Executions per second |
---|---|
Image | 181801232.0 Ops/sec |
ImageBitmap | 177765984.0 Ops/sec |
Canvas | 178320576.0 Ops/sec |
The benchmark provided tests the performance of rendering images in an HTML5 canvas using three different approaches: rendering through an Image
, an ImageBitmap
, and using another Canvas
element. Below is an explanation of each method compared, their pros and cons, and other considerations.
Image
Image
object.drawImage
method is called, which can result in performance overhead, especially for large images.ImageBitmap
ImageBitmap
, which is created asynchronously using the createImageBitmap()
function.ImageBitmap
is designed to be more performant as it allows the browser to decode the image ahead of time and optimize rendering.Canvas
The benchmark results show the number of executions per second for each approach on a specific setup (Windows, Chrome 135):
Use Cases:
Image
for straightforward image rendering without complex requirements.ImageBitmap
when performance is critical, especially in scenarios with high-frequency or real-time rendering, such as games or dynamic visualizations.Canvas
when you need to manipulate and render from other canvas elements, such as combining multiple graphic objects.Browser Support: All three approaches are widely supported in modern browsers, but implementation details (like performance optimizations) may vary.
Memory Management: Developers should pay attention to memory usage when using ImageBitmap
, especially when working with a large number of images or when the images are high resolution.
Understanding the pros and cons of each option helps developers choose the best approach for their specific use cases, balancing performance, complexity, and ease of integration into their applications.