<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;
$ctx.putImageData($px, px.x, px.y);
}
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);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fillRect/concat | |
fillRect/join | |
1×1 Image Data | |
fillRect/backtick |
Test name | Executions per second |
---|---|
fillRect/concat | 849.8 Ops/sec |
fillRect/join | 811.6 Ops/sec |
1×1 Image Data | 20.0 Ops/sec |
fillRect/backtick | 849.4 Ops/sec |
Let's break down the benchmark and its various components.
Benchmark Definition
The benchmark is designed to test the performance of different ways to set a pixel color on an HTML5 canvas element using JavaScript. The script preparation code creates a 2D context ($ctx
) on a canvas element with specific dimensions (800x300 pixels) and clears the canvas. It then generates an array of pixel data ($pxls
) containing random RGB values.
Individual Test Cases
There are four test cases, each comparing a different approach to set a single pixel color on the canvas:
fillRect/concat
: Uses a simple string concatenation to build the RGBA color string.fillRect/join
: Utilizes the built-in join()
method to concatenate the RGB values into a single string.1×1 Image Data
: Uses the putImageData()
method to set the pixel data directly on the canvas context.fillRect/backtick
: Employs template literals (backticks) to format the RGBA color string.Options Compared
The test cases compare the performance of different approaches to set a single pixel color:
join()
for building the RGBA color stringputImageData()
vs. simple string concatenation or backtick formattingPros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
fillRect/concat
):fillRect/join
):join()
method implementation.join()
support.1×1 Image Data
):putImageData()
method's behavior.Other Considerations
fillRect/backtick
is a relatively new feature introduced in JavaScript 2015. While it provides a more concise way to format strings, its performance impact may vary depending on the specific browser and implementation.Alternatives
Other alternatives that could be explored include:
canvas.toDataURL()
to generate an image URL from the pixel data, which might offer a different performance profile compared to setting individual pixels directly.requestAnimationFrame()
, Web Workers).Keep in mind that these alternatives would require additional benchmarking and testing to validate their performance impact.