var img = new Image();
var canvas = document.createElement('canvas');
var resCanvas = document.createElement('canvas');
var isReady = false;
var frameRaw1 = null;
var frameRaw2 = null;
var result = null;
img.addEventListener('load', function() {
createImageBitmap(img).then(function(imageBitmap) {
frameRaw2 = imageBitmap;
canvas.width = frameRaw2.width;
canvas.height = frameRaw2.height;
frameRaw1 = canvas.getContext('2d');
resCanvas.width = frameRaw2.width;
resCanvas.height = frameRaw2.height;
result = resCanvas.getContext('2d');
isReady = true;
});
});
img.src = 'https://cdn.britannica.com/20/194520-050-DCAE62F1/New-World-Sylvilagus-cottontail-rabbits.jpg';
frameRaw1.clearRect(0, 0, canvas.width, canvas.height);
frameRaw1.drawImage(img, 0, 0);
result.drawImage(canvas, 0, 0);
createImageBitmap(img).then(function(r) {
frameRaw2 = imageBitmap;
result.drawImage(frameRaw2, 0, 0);
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
canvas2d.drawImage(imageElement) | |
createImageBitmap(imageElement) |
Test name | Executions per second |
---|---|
canvas2d.drawImage(imageElement) | 8200.7 Ops/sec |
createImageBitmap(imageElement) | 90.0 Ops/sec |
Let's break down the provided benchmark definition and explain what is being tested, compared, and the pros/cons of each approach.
Benchmark Definition
The benchmark measures the performance difference between two approaches:
canvas2d.drawImage(imageElement)
: This method uses the 2D drawing context of a canvas element to draw an image onto it.createImageBitmap(imageElement)
: This method creates a bitmap image from an HTML image element, which can then be used as a 2D drawing context.Script Preparation Code
The script preparation code sets up the environment for both tests:
img
) and two canvases (canvas
and resCanvas
).Html Preparation Code
The HTML preparation code is not provided, but it's likely that a simple HTML page with a single <img>
element is being used as the input for this benchmark.
Test Cases
There are two test cases:
canvas2d.drawImage(imageElement)
: This test case draws the image onto the canvas
and then draws the entire canvas using the resulting 2D drawing context.createImageBitmap(imageElement)
: This test case creates a bitmap image from the input image element, which is then used as a 2D drawing context.Comparison
The benchmark compares the execution time of each approach:
canvas2d.drawImage(imageElement)
: This method uses the canvas element's native 2D drawing capabilities to draw and transform images.createImageBitmap(imageElement)
: This method creates a new bitmap image from the input image, which can then be used as a 2D drawing context.Pros and Cons
canvas2d.drawImage(imageElement)
:
Pros:
Cons:
createImageBitmap(imageElement)
:
Pros:
Cons:
Library and Purpose
createImageBitmap
is a part of the Web API and is implemented by most modern browsers.CanvasRenderingContext2D
interface is also a part of the Web API and provides native 2D drawing capabilities for canvases.Special JS Feature or Syntax
The benchmark uses ES6+ syntax (e.g., arrow functions, template literals) and is compatible with modern JavaScript engines.
Alternatives
Some alternatives to this benchmark could include:
canvas2d.drawImage(imageElement)
with other 2D drawing methods, such as drawImage()
or putImageData()
.