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://media.discordapp.net/attachments/447410261289205781/574359442913492992/unknown.png';
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) | 0.0 Ops/sec |
createImageBitmap(imageElement) | 23409.0 Ops/sec |
Let's break down what is tested in the provided JSON benchmark.
The test is comparing two approaches:
canvas2d.drawImage(imageElement)
createImageBitmap(imageElement)
What are these approaches?
canvas2d.drawImage(imageElement)
is a low-level method that draws an image on a 2D canvas context. It requires manual management of the image's pixels, which can be slow and error-prone.createImageBitmap(imageElement)
is a higher-level API that creates a bitmap representation of an image element. This allows for faster rendering and manipulation of images without the need for manual pixel management.Pros and Cons
canvas2d.drawImage(imageElement)
createImageBitmap(imageElement)
Library:
In this benchmark, createImageBitmap
is a Web API provided by modern browsers. It allows developers to create bitmap representations of images without having to manually manage pixels. This library provides a convenient and efficient way to work with images in web applications.
Special JS feature or syntax:
None mentioned in the benchmark definition. However, it's worth noting that the use of async/await
in the script preparation code is a modern JavaScript feature introduced in ECMAScript 2017 (ES7). This syntax allows for more concise and readable asynchronous code.
Alternatives:
Other alternatives to these approaches include:
createImageBitmap
API.In general, the choice of approach depends on the specific requirements and constraints of your project. If you need fine-grained control over image rendering and manipulation, canvas2d.drawImage(imageElement)
might be a better choice. However, if speed and convenience are more important, createImageBitmap(imageElement)
could be a more suitable option.