<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 | 432.5 Ops/sec |
fillRect/join | 353.4 Ops/sec |
1×1 Image Data | 1488.6 Ops/sec |
Let's break down the provided JSON data for MeasureThat.net and explain what is tested, compared options, pros and cons of each approach, library usage, special JavaScript features or syntax, and alternative approaches.
Benchmark Definition
The benchmark defines three test cases:
fillStyle
property instead of joining an array.Comparison Options
The three test cases differ in how they manipulate the RGBA value for the fillStyle
property:
'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')'
) to build the RGBA string.[px.r,px.g,px.b,px.a/255].join()
) to build the RGBA string.Pros and Cons of each approach
Library usage
There is no explicit library usage in these test cases. However, Canvas
and 2D
contexts are part of the standard JavaScript API, which makes this benchmark relevant to most web developers.
Special JavaScript features or syntax
There are no special JavaScript features or syntax used in these test cases, other than using let
(not shown) for variable declaration.
Alternative approaches
Other alternative approaches could include:
Keep in mind that these alternative approaches are not directly related to the specific test cases presented here, but rather to broader considerations for performance optimization and code reuse.