<canvas id='master' width='1000' height='1000'></canvas>
<canvas id='pid' width='1000' height='1000'></canvas>
<canvas id='pid2' width='1000' height='1000'></canvas>
var canvas = document.getElementById('master');
var ctx = canvas.getContext('2d');
var pid = document.getElementById('pid').getContext('2d');
var pid2 = document.getElementById('pid2').getContext('2d');
var imgData;
var width = 1000;
var height = 1000;
ctx.width = width;
ctx.height = height;
ctx.fillStyle = "red";
// DRAW SOME BLOCKS
for (let i=0; i<width; i+=50) {
ctx.fillRect(i,0,50,50);
ctx.fillRect(i,(height/2)-25,50,50);
ctx.fillRect(i,height-50,50,50);
}
for (let i=0; i<height; i+=50) {
ctx.fillRect(0,i,50,50);
ctx.fillRect((width/2)-25,i,50,50);
ctx.fillRect(width-50,i,50,50);
}
// PRE-COPY IMG DATA
imgData = ctx.getImageData(0,0,width,height);
pid.drawImage(canvas,0,0);
pid2.putImageData(imgData,0,0);
let imgDataLocal = ctx.getImageData(0,0,width,height);
pid2.putImageData(imgDataLocal,0,0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DrawImage copy canvas -> canvas | |
PutImageData copy imgData -> canvas | |
PutImageData copy canvas -> imgData -> canvas |
Test name | Executions per second |
---|---|
DrawImage copy canvas -> canvas | 181618.1 Ops/sec |
PutImageData copy imgData -> canvas | 570.8 Ops/sec |
PutImageData copy canvas -> imgData -> canvas | 345.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Definition and Test Cases
The benchmark is designed to compare three different approaches for copying image data from one canvas to another:
drawImage
method of the context
object to draw an image on another canvas.putImageData
method of the context
object to paste image data onto a canvas.get imageData
method, and then pasting it onto another canvas.Options Compared
The three approaches are compared in terms of their execution time (measured in executions per second).
Pros and Cons of Each Approach
drawImage
.Library and Purpose
None of the test cases rely on any specific libraries beyond the standard JavaScript library. However, it's worth noting that the Canvas
API is a modern web standard for rendering 2D graphics in HTML5.
Special JS Feature or Syntax
The benchmark does not explicitly use any special JavaScript features or syntax beyond the standard JavaScript library and Canvas API.
Other Alternatives
If you're looking for alternative approaches to copying image data, you may consider:
putImageData
.Keep in mind that these alternatives may have their own trade-offs and requirements, such as additional setup or complexity in implementation.