var img = new Image();
var imageBitmap;
var doneLoading = false;
let canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
let canvas2 = document.createElement('canvas');
canvas2.width = 640;
canvas2.height = 480;
var ctx = canvas.getContext('2d');
var ctx2 = canvas2.getContext('2d');
img.addEventListener('load', function() {
Promise.all([
createImageBitmap(img)
]).then(function(images) {
imageBitmap = images[0];
doneLoading = true;
}, false);
});
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg';
if (doneLoading)
ctx.drawImage(img,0,0);
if (doneLoading)
ctx2.drawImage(imageBitmap,0,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
ImageBitmap |
Test name | Executions per second |
---|---|
Image | 15470103.0 Ops/sec |
ImageBitmap | 499889.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark is comparing two approaches for rendering images on a web page:
Image
object: This approach involves creating an instance of the Image
object, setting its src
attribute to an image URL, and then drawing it onto a canvas element.ImageBitmap
to render an image: This approach uses the createImageBitmap()
method to convert the raw image data into a bitmap format, which can be used by the canvas element.Options Compared
The two options being compared are:
Image
object using ctx.drawImage()
.ImageBitmap
with ctx2.drawImage()
.Pros and Cons of Each Approach
Image
object:ImageBitmap
to render an image:ImageBitmap
format. However, it might not be as efficient for very large or complex images.Library and Purpose
The benchmark uses the following library:
createImageBitmap()
: A Web API method created by Mozilla to convert raw image data into a bitmap format, which can be used by canvas elements.ImageBitmap
format.Special JS Feature or Syntax
The benchmark uses the following special feature:
Promise.all()
: A built-in JavaScript function used to execute multiple asynchronous operations concurrently and wait for their completion.createImageBitmap()
operation and drawing the raw image using ctx.drawImage()
simultaneously, ensuring that the benchmark is run only after both operations are complete.Other Alternatives
If you're interested in exploring alternative approaches, consider the following options:
ImageBitmap
format.