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');
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() {
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 | 257576.2 Ops/sec |
ImageBitmap | 247192.0 Ops/sec |
Canvas | 252611.8 Ops/sec |
The benchmark you've provided measures the performance of rendering images in a 2D canvas context using three different approaches: the standard HTML Image
, ImageBitmap
, and another canvas element as the source.
Image Rendering:
ctxImage.drawImage(img,0,0); ctxImage.clearRect(0,0,700,700);
Image
object to draw the image onto the canvas. The drawImage
method is called to render the image, followed by clearing the canvas.ImageBitmap Rendering:
ctxImageBitmap.drawImage(imageBitmap,0,0); ctxImageBitmap.clearRect(0,0,700,700);
ImageBitmap
is used, which is a more optimized representation of an image for drawing operations. The bitmap is prepared asynchronously using createImageBitmap()
, which allows for faster rendering performance as it provides a ready-to-use image for manipulation.Canvas Rendering:
ctxCanvas.drawImage(canvas,0,0); ctxCanvas.clearRect(0,0,700,700);
The test results show the number of executions per second for each method:
Image:
Image
can introduce latency due to the need for the image to be fully loaded before rendering.ImageBitmap:
Canvas:
createImageBitmap()
: This is a web API function used to create an ImageBitmap
from an HTMLImageElement
, HTMLCanvasElement
, or Blob
. This function prepares an image more optimally for rendering in a canvas context.Canvas with OffscreenCanvas: Instead of using the visible canvas in the DOM, an OffscreenCanvas
can be utilized for rendering in web workers, which might provide better performance depending on the use case, especially for complex renderings.
WebGL: For more advanced rendering tasks, WebGL can be considered, allowing for 3D rendering and better GPU utilization. It can be more efficient for certain applications but has a steeper learning curve.
CSS Graphics: CSS3 can also be used for simpler shapes and effects, but for image rendering, it lacks the versatility provided by the Canvas API.
In summary, the benchmark provides valuable insights into the performance implications of using different methods for image rendering in the canvas. Understanding the strengths and weaknesses of these different approaches helps in making informed decisions tailored to specific application requirements.