var img = new Image();
var canvas = document.createElement('canvas');
var isReady = false;
var frameRaw1 = null;
var frameRaw2 = null;
img.addEventListener('load', function() {
createImageBitmap(img).then(function(imageBitmap) {
frameRaw2 = imageBitmap;
canvas.width = frameRaw2.width;
canvas.height = frameRaw2.height;
frameRaw1 = canvas.getContext('2d');
isReady = true;
});
});
img.src = 'https://media.discordapp.net/attachments/447410261289205781/574359442913492992/unknown.png';
frameRaw1.clearRect(0, 0, canvas.width, canvas.height);
frameRaw1.drawImage(img, 0, 0);
createImageBitmap(img).then(function(imageBitmap) {
frameRaw2 = imageBitmap;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
canvas2d.drawImage(imageElement) | |
createImageBitmap(imageElement) |
Test name | Executions per second |
---|---|
canvas2d.drawImage(imageElement) | 0.0 Ops/sec |
createImageBitmap(imageElement) | 201888.6 Ops/sec |
Let's dive into the explanation of what's being tested in this benchmark.
Benchmark Purpose
The purpose of this benchmark is to compare the performance of two approaches:
canvas.getContext('2d').drawImage()
createImageBitmap()
These two approaches are compared because they have different use cases and may differ in terms of performance, memory usage, and complexity.
Options Compared
The benchmark compares the execution time of each approach:
canvas2d.drawImage(imageElement)
: This option uses the 2D canvas API to draw the image. It involves creating a canvas element, setting its dimensions based on the image's size, and then drawing the image onto it using drawImage()
.createImageBitmap(imageElement)
: This option uses the createImageBitmap()
function to convert the image to a bitmap. The resulting bitmap can be used for various purposes, such as pixel manipulation or caching.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
canvas2d.drawImage(imageElement)
createImageBitmap(imageElement)
canvas2d.drawImage()
for simple rendering tasks.Library and Purpose
The benchmark uses the following libraries:
createImageBitmap()
: This function is part of the Web API, introduced in Chrome 78 (released in April 2019). It provides a way to convert an image into a bitmap that can be used for various purposes.canvas.getContext('2d')
: This method is also part of the Web API and allows accessing the 2D drawing context of a canvas element.Special JS Features or Syntax
This benchmark uses the following special JavaScript features:
async/await
: Used to handle the promise returned by createImageBitmap()
.try-catch
blocks: Not used explicitly, but it's implied that the benchmark should catch any errors that might occur during execution.Alternatives
If you're interested in exploring alternative approaches or want to see how this benchmark performs on other browsers or devices, here are a few options:
Web Workers
: You could use Web Workers to run the benchmarks concurrently and compare the performance of each approach in a multi-threaded environment.canvas2d.drawImage()
and createImageBitmap()
with other image processing APIs, such as WebGL or Pixi.js, to see which one performs best for specific use cases.Keep in mind that these alternatives might require additional setup and testing, but they can provide valuable insights into the performance characteristics of each approach.