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;
let canvas3 = document.createElement('canvas');
canvas3.width = 640;
canvas3.height = 480;
var ctx = canvas.getContext('2d');
var ctx2 = canvas2.getContext('2d');
var ctx3 = canvas2.getContext('2d');
var cv = document.createElement('canvas');
cv.width = 700;
cv.height = 700;
var cvx = cv.getContext('2d');
img.addEventListener('load', function() {
cvx.drawImage(img,0,0);
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);
if (doneLoading)
cvx.drawImage(cv,0,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
ImageBitmap | |
canvas |
Test name | Executions per second |
---|---|
Image | 344179.7 Ops/sec |
ImageBitmap | 84715.1 Ops/sec |
canvas | 1762.8 Ops/sec |
Let's dive into the details of this benchmark.
Benchmark Overview
The provided JSON represents a JavaScript microbenchmark that compares the performance of three different approaches to draw an image:
The benchmark is designed to test the number of executions per second (ExecutionsPerSecond) for each approach.
Options Compared
The three options being compared are:
drawImage()
function on the img
object.createImageBitmap()
function and then draws the image using the drawImage()
function on an ImageBitmap object.drawImage()
function.Pros and Cons of Each Approach
createImageBitmap()
function to convert the image to a more convenient format, which can improve performance for some use cases.Library Used
The createImageBitmap()
function is used in the ImageBitmap test case. This function converts an image into a bitmap format that can be manipulated as a 2D array of pixels.
Special JavaScript Feature/Syntax
There is no special JavaScript feature or syntax being tested in this benchmark. The code is standard JavaScript and uses only native functions and features.
Other Alternatives
If you wanted to test different approaches, you could consider the following alternatives:
Overall, this benchmark provides a good starting point for comparing the performance of different approaches to drawing images in JavaScript. By testing each approach with and without the ImageBitmap conversion, you can get a better understanding of their relative performance characteristics.