<canvas align="center" id="canvas" width="800" height="800"></canvas>
var ctx = document.getElementById('canvas').getContext('2d');
ctx.fillStyle = 'rgba(0,1,0,1)';
var rects = []
for (var i = 0; i < 50; i++) {
var width = 5 + i * 5;
var height = 7 + i * 3;
var path = new Path2D();
path.rect(0, 0, width, height);
var rect =
{
x: i,
y: i,
width: width,
height: height,
cachedPath: path
};
rects.push(rect);
}
ctx.clearRect(0, 0, 500, 500);
for (var i = 0; i < rects.length; i++) {
var rect = rects[i];
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
}
for (var i = 0; i < rects.length; i++) {
var rect = rects[i];
ctx.strokeStyle = '#000000';
ctx.lineWidth = 1;
ctx.translate(rect.x, rect.y);
ctx.beginPath();
ctx.rect(rect.x, rect.y, rect.width, rect.height);
ctx.stroke();
ctx.translate(-rect.x, -rect.y);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Direct draw | |
Direct with beginpath |
Test name | Executions per second |
---|---|
Direct draw | 15359.2 Ops/sec |
Direct with beginpath | 10065.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition:
The provided benchmark definition is for measuring the performance of drawing paths on a canvas element using 2D graphics. The script preparation code sets up a 2D drawing context, fills it with a blue color (RGBA: 0,1,0,1), and creates an array of rectangles (rects
) to draw. Each rectangle has a varying width and height.
Script Preparation Code:
var ctx = document.getElementById('canvas').getContext('2d');
ctx.fillStyle = 'rgba(0,1,0,1)';
var rects = [];
for (var i = 0; i < 50; i++) {
var width = 5 + i * 5;
var height = 7 + i * 3;
var path = new Path2D();
path.rect(0, 0, width, height);
var rect = {
x: i,
y: i,
width: width,
height: height,
cachedPath: path
};
rects.push(rect);
}
ctx.clearRect(0, 0, 500, 500);
Html Preparation Code:
<canvas align="center" id="canvas" width="800" height="800"></canvas>
This code creates a canvas element and sets up a 2D drawing context. It then generates an array of rectangles with varying dimensions, each with a cachedPath
property containing a Path2D object representing the rectangle's shape.
Individual Test Cases:
There are two test cases:
for (var i = 0; i < rects.length; i++) {
var rect = rects[i];
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
}
This test case uses the fillRect
method to draw each rectangle four times: once for each corner. This is a straightforward approach that directly draws the rectangles without any additional processing.
for (var i = 0; i < rects.length; i++) {
var rect = rects[i];
ctx.strokeStyle = '#000000';
ctx.lineWidth = 1;
ctx.translate(rect.x, rect.y);
ctx.beginPath();
ctx.rect(rect.x, rect.y, rect.width, rect.height);
ctx.stroke();
ctx.translate(-rect.x, -rect.y);
}
This test case uses the beginPath
method to create a new path and then draws each rectangle using stroke
. The translate
method is used to move the drawing context to the top-left corner of each rectangle, ensuring that the rectangle is drawn correctly. This approach requires more processing than the direct draw method but produces a more accurate result.
Pros and Cons:
fillRect
for each rectangleLibrary and Purpose:
The Path2D
object is used in the script preparation code. It represents a 2D path that can be used to create complex shapes, including rectangles, arcs, and curves.
Special JS Feature or Syntax:
There is no special JavaScript feature or syntax being used in this benchmark definition. However, note that the var
keyword is being used with i
in both test cases, which may not be the best practice in modern JavaScript coding standards (e.g., using let
and const
instead).
Alternatives:
Other alternatives for drawing shapes on a canvas element include:
canvas.getContext('2d').drawImage()
method to draw images or use image data.Keep in mind that these alternatives may have different performance characteristics, dependencies on specific browser versions, and additional complexity compared to the simple drawing methods used in this benchmark definition.