Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixe3
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/optimized further
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<canvas id="c" width="800" height="300"></canvas>
Script Preparation code:
$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;
Tests:
fillRect/concat
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); }
fillRect/optimized further
var i=500, px=null, alphmult = 1 / 255 for (;i--;){ px = $pxls[$i++ % 10000]; $ctx.fillStyle = `rgba(${px.r},${px.g},${px.b},${px.a * alphmult})`; $ctx.fillRect(px.x, px.y, 1, 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
fillRect/concat
fillRect/optimized further
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark defines a script preparation code that sets up a 2D canvas context (`$ctx`) on an HTML element with an ID of "c". The canvas has a width of 800 pixels and a height of 300 pixels. A random image data is created using `createImageData(1, 1)` and stored in `$pxls`. This image data consists of 10,000 pixel values with random red, green, blue, and alpha channels. **Script Preparation Code** The script preparation code is: ```javascript $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; ``` This code sets up the canvas context, clears it, creates an image data object, and populates `$pxls` with random pixel values. **Html Preparation Code** The HTML preparation code is: ```html <canvas id="c" width="800" height="300"></canvas> ``` This code creates a canvas element with an ID of "c", setting its width to 800 pixels and its height to 300 pixels. **Individual Test Cases** There are two individual test cases: 1. `fillRect/concat`: ```javascript for (var i=500;i--;){\r\nvar px = $pxls[$i++ % 10000];\r\n$ctx.fillStyle = 'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')';\r\n$ctx.fillRect(px.x, px.y, 1, 1);\r\n} ``` This test case uses the `fillStyle` property to set the color of the fill, concatenating the red, green, blue, and alpha values. It then fills a rectangle with the specified color at the corresponding pixel coordinates. 2. `fillRect/optimized further`: ```javascript var i=500, px=null, alphmult = 1 / 255\r\nfor (;i--;){\r\npx = $pxls[$i++ % 10000];\r\n$ctx.fillStyle = `rgba(${px.r},${px.g},${px.b},${px.a * alphmult})`;\r\n$ctx.fillRect(px.x, px.y, 1, 1);\r\n} ``` This test case is similar to the previous one, but it uses template literals to construct the color string and multiplies the alpha value by a constant factor (`alphmult`) to optimize performance. **Options Compared** The two test cases compare different approaches to filling rectangles with the same pixel values: * `fillRect/concat`: uses concatenation to construct the color string * `fillRect/optimized further`: uses template literals and multiplies the alpha value by a constant factor **Pros and Cons** * `fillRect/concat`: + Pros: simple, easy to understand + Cons: may be slower due to concatenation overhead * `fillRect/optimized further`: + Pros: optimized for performance (alpha multiplication) + Cons: uses template literals, which may be less intuitive **Other Considerations** * Both test cases assume that the canvas context is already set up and cleared. * The `$pxls` array is populated with random pixel values, which are used to determine the color of each rectangle. **Alternatives** If you want to compare other approaches to filling rectangles, you could consider adding additional test cases: * Using a fixed color instead of concatenating or multiplying alpha values * Using a different data structure (e.g., an array of objects with `x`, `y`, `r`, `g`, `b`, and `a` properties) * Using a different library or framework for rendering the rectangles
Related benchmarks:
Setting Canvas Pixe
Setting Canvas Pixel123
Setting Canvas Pixel 4
Setting Canvas Pixel 3
Comments
Confirm delete:
Do you really want to delete benchmark?