<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(800, 300);
$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 | 550.7 Ops/sec |
fillRect/join | 407.5 Ops/sec |
1×1 Image Data | 15.9 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Purpose
The test is designed to measure the performance of different ways to draw pixels on a canvas using JavaScript. Specifically, it compares three approaches:
fillStyle
with concatenated strings (fillRect/concat
)fillStyle
with an array expression (fillRect/join
)putImageData
with an image data array (1×1 Image Data
)Options Comparison
The benchmark tests these three options because they are common ways to draw pixels on a canvas using JavaScript. Here's a brief analysis of each option:
fillStyle
string, which can lead to slower performance due to the overhead of creating and manipulating strings.fillStyle
string, which is more efficient than concatenation because it avoids creating intermediate strings. However, it may still have some overhead due to the creation and manipulation of arrays.putImageData
to draw a single pixel at a time. It's likely the fastest option because it avoids any overhead related to string manipulation or array creation.Pros and Cons
Here are some pros and cons of each approach:
Library Usage
In this benchmark, no libraries are explicitly mentioned as being used. However, it's worth noting that putImageData
is a standard JavaScript method for drawing images on a canvas, so it doesn't introduce any external dependencies.
Special JS Features/Syntax
There are no special JavaScript features or syntax being tested in this benchmark. The code uses standard JavaScript syntax and features, such as string concatenation, array expressions, and putImageData
.
Alternatives
If you're looking for alternatives to these approaches, here are a few options:
fillRect/concat
, you could use the createCanvas
method provided by modern browsers (e.g., const canvas = document.createElement('canvas');
) or a library like CanvasJS.fillRect/join
, you could consider using a faster string manipulation method, such as using a StringBuilder
class (available in some JavaScript engines).1×1 Image Data
, you could use other methods for drawing pixels on a canvas, such as using the createPixelArray
method or a library like Pixi.js.Overall, this benchmark provides a useful comparison of different approaches to drawing pixels on a canvas using JavaScript. By understanding the pros and cons of each option, developers can choose the fastest and most efficient approach for their specific use case.