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);
});
img.src = 'https://1.bp.blogspot.com/-52MtzD0GfX0/WvP52CL1WjI/AAAAAAAAOVw/_OpK4JHeWK01d-7IiZ6vzojYGhXqLRXrACLcBGAs/s1600/EMxediL.jpg';
if (doneLoading)
ctx.drawImage(img,0,0);
if (doneLoading)
ctx3.drawImage(cv,0,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Image | |
canvas |
Test name | Executions per second |
---|---|
Image | 5597939.5 Ops/sec |
canvas | 5652960.0 Ops/sec |
Benchmark Explanation
The provided JSON represents a benchmark test case for measuring the performance of different approaches to draw images on a canvas or using an ImageBitmap.
Options Compared
There are two options being compared:
drawImage
method to draw an image on a 2D drawing context (ctx
). The image is loaded from a URL using the src
attribute of the Image
object.drawImage
method to draw an image on another canvas element.Pros and Cons
drawImage
method by leveraging the GPU acceleration of ImageBitmapdrawImage
methodLibrary and Purpose
Special JS Feature or Syntax
There is no special JavaScript feature or syntax being utilized in this benchmark test case. The focus is on the performance comparison between two approaches using standard JavaScript APIs.
Other Alternatives
Additional Considerations
The choice between these approaches can depend on various factors, such as:
Overall, the Image vs ImageBitmap vs cv with context2d benchmark test case provides a useful comparison of two common approaches to drawing images in JavaScript, highlighting the trade-offs between simplicity and performance.