<canvas id="c" width="800" height="300"></canvas>
$c = document.getElementById('c');
$ctx = $c.getContext('2d');
$ctx.clearRect(0, 0, 800, 300);
$px = $ctx.createImageData(1, 1);
$pxls = [];
for (var i=0; i<10000; ++i) $pxls.push({
x: Math.random() * 800 << 0,
y: Math.random() * 300 << 0,
r: Math.random() * 255 << 0,
g: Math.random() * 255 << 0,
b: Math.random() * 255 << 0,
a: Math.random() * 128 << 0 + 128
});
$i = 0;
for (var i=500;i--;){
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = 'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')';
$ctx.fillRect(px.x, px.y, 1, 1);
}
for (var i=500;i--;){
var px = $pxls[$i++ % 10000];
$ctx.fillStyle = 'rgba(' + [px.r,px.g,px.b,px.a/255].join() + ')';
$ctx.fillRect(px.x, px.y, 1, 1);
}
for (var i=500;i--;){
var px=$pxls[$i++ % 10000], d=$px.data;
d[0] = px.r;
d[1] = px.g;
d[2] = px.b;
d[3] = px.a;
$ctx.putImageData($px, px.x, px.y);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fillRect/concat | |
fillRect/join | |
1×1 Image Data |
Test name | Executions per second |
---|---|
fillRect/concat | 2764.6 Ops/sec |
fillRect/join | 2572.4 Ops/sec |
1×1 Image Data | 220.5 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Overview
The benchmark measures the performance of different approaches for drawing a single pixel on a 2D canvas using JavaScript. The test case uses a predefined set of 10,000 pixels with random red, green, blue, and alpha values. The benchmark compares three different methods:
fillRect/concat
fillRect/join
1×1 Image Data
Options Comparison
fillRect/concat
This method uses string concatenation to build the rgba
color string.
$ctx.fillStyle = 'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')';
Pros:
Cons:
fillRect/join
This method uses the join()
method to concatenate the color components into a single string.
$ctx.fillStyle = 'rgba(' + [px.r, px.g, px.b, px.a/255].join() + ')';
Pros:
Cons: None notable.
1×1 Image Data
This method uses the putImageData
function to draw a single pixel from an image data array.
$ctx.putImageData($px, px.x, px.y);
Pros:
Cons:
Library Used
None.
Special JS Feature or Syntax
The benchmark uses the following features:
px.r
is interpolated into a string)However, the putImageData
function is not a new feature introduced in modern JavaScript versions. It's an older API that was introduced in HTML 5.
Other Alternatives
If you wanted to test alternative methods, you could consider:
Keep in mind that these alternatives may introduce additional complexity and dependencies compared to the original benchmark.
Benchmark Preparation Code Explanation
The script preparation code sets up a canvas element with an ID of "c" and creates a 2D drawing context ($ctx
) for it. It then clears the canvas, creates a new image data object ($px
), and populates it with random pixel values ($pxls
). The script loop increments through the array of pixels, drawing each one on the canvas using the specified method.
Html Preparation Code Explanation
The HTML preparation code defines an empty <canvas>
element with an ID of "c" and sets its width to 800 and height to 300.