<canvas id='master' width='100' height='100'></canvas>
<canvas id='pid' width='100' height='100'></canvas>
<canvas id='di' width='100' height='100'></canvas>
var master = document.getElementById('master');
var masterctx = master.getContext('2d');
var pid = document.getElementById('pid').getContext('2d');
var di = document.getElementById('di').getContext('2d');
masterctx.fillRect(0,0,50,50);
masterctx.fillStyle = "red";
masterctx.fillRect(50,50,100,100);
let imgData1 = masterctx.getImageData(0,0,100,100);
let imgData2 = masterctx.getImageData(25,0,75,100);
di.drawImage(master,0,0);
pid.putImageData(imgData1,0,0);
di.drawImage(master,25,0,50,100,25,0,50,100);
pid.putImageData(imgData2,25,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DrawImage whole master into di canvas | |
PutImageData whole master into pid canvas | |
DrawImage half master into di canvas | |
PutImageData half master into pid canvas |
Test name | Executions per second |
---|---|
DrawImage whole master into di canvas | 71621.4 Ops/sec |
PutImageData whole master into pid canvas | 9928.4 Ops/sec |
DrawImage half master into di canvas | 65644.7 Ops/sec |
PutImageData half master into pid canvas | 8833.1 Ops/sec |
The benchmark tests the performance of two different methods for rendering images onto a canvas in HTML5: drawImage
and putImageData
. These methods differ in how they handle the image data being drawn, and the benchmark compares their performance when working with a base image drawn on a master canvas.
DrawImage Method:
Test Cases:
di.drawImage(master, 0, 0);
di.drawImage(master, 25, 0, 50, 100, 25, 0, 50, 100);
Description: The drawImage
function is a more straightforward method to copy an image or a part of an image from one canvas to another. In this benchmark, it tests both the full image from the master canvas and a subsection of the image.
Pros:
Cons:
PutImageData Method:
Test Cases:
pid.putImageData(imgData1, 0, 0);
pid.putImageData(imgData2, 25, 0);
Description: putImageData
is used to draw a rectangular area of pixel data onto the canvas. In this benchmark, it takes extracted pixel image data from the master canvas and puts it into a second canvas.
Pros:
Cons:
drawImage
for straightforward image copying tasks due to additional overhead of handling pixel data directly.The benchmark results reveal the executions per second for each of the test cases performed in Chrome 133 on a Windows Desktop environment:
drawImage
, resulting in approximately 603,849 executions per second.drawImage
, achieving 590,539 executions per second.putImageData
calls (both full and half images) performed significantly slower, with the fastest being around 57,760 executions per second for the half image and only 53,754 for the full image.When deciding between drawImage
and putImageData
, developers may also consider the following:
drawImage
is typically preferred. However, if there's a requirement for direct pixel manipulation or performance enhancements through preprocessing of pixel data, putImageData
is a better choice.In summary, this benchmark explores the performance trade-offs between using drawImage
, which is generally faster and easier for copying images, and putImageData
, which offers more granular control over pixel manipulation but comes with a performance penalty. Depending on the specific requirements of a project, developers can choose the most suitable approach based on these performance characteristics and their specific use case.