<canvas id="canvas" width="1400" height="1400"></canvas>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext("2d");
var cacheG = document.createElement('canvas');
var cacheD = cacheG.getContext('2d');
cacheG.width = 1200;
cacheG.height = 1200;
cacheD.beginPath();
cacheD.fillStyle = '#000';
cacheD.arc(600, 600, 600, 0, Math.PI * 2, true);
cacheD.fill();
var d = cacheD.getImageData(0,0,1200,1200)
ctx.drawImage(cacheG, 6, 6);
ctx.putImageData(d, 6, 6);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
drawImage | |
putImageData |
Test name | Executions per second |
---|---|
drawImage | 28152.2 Ops/sec |
putImageData | 1454.1 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
Benchmark Definition JSON
The benchmark definition represents two microbenchmarks: drawImage
and putImageData
. These tests aim to measure the performance difference between drawing an image using drawImage()
and putting image data into context using putImageData()
.
Script Preparation Code
The script preparation code creates a temporary canvas element (cacheG
) with a size of 1200x1200. It then sets up a 2D drawing context on this canvas (cacheD
). The code draws a black circle on the canvas using the fillStyle
and arc()
methods.
Html Preparation Code
The HTML preparation code includes an <canvas>
element with a width and height of 1400 pixels, which will be used as the container for the benchmark.
Individual Test Cases
There are two test cases:
ctx.drawImage(cacheG, 6, 6);
. The image is drawn at position (6, 6) on the main canvas.ctx.putImageData(d, 6, 6);
. The image data (d
) is obtained from the temporary canvas (cacheD
) and drawn at position (6, 6) on the main canvas.Library: Canvas
Both tests use the HTML5 Canvas API, which provides a way to draw graphics in web pages. The Canvas API allows developers to create images, manipulate pixel data, and perform various drawing operations using JavaScript.
In this benchmark, the Canvas
library is used to:
cacheG
) for image dataSpecial JS Feature/Syntax:
The tests use a special JavaScript feature called ImageData
objects, which represent pixel data in the form of a 1D array. The ImageData
object is used to store the pixel data from the temporary canvas (cacheD
) and pass it to the putImageData()
method for drawing.
Pros and Cons:
The pros of using drawImage()
over putImageData()
are:
drawImage()
is generally faster than putting image data into context.drawImage()
has less overhead compared to putImageData()
, as it doesn't require manipulating pixel data.However, the cons of using drawImage()
over putImageData()
are:
drawImage()
can lead to more complex code, as developers need to handle image loading and caching.drawImage()
, developers have less control over the drawing process compared to putImageData()
.On the other hand, putImageData()
provides:
putImageData()
.putImageData()
can be used for more complex graphics and animations.Alternatives:
Other alternatives to testing performance differences between drawImage()
and putImageData()
include:
fillRect()
, strokeText()
, etc.Keep in mind that the choice of test case and benchmarking approach depends on the specific use case and requirements.