Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel 2
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data
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/join
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); }
1×1 Image Data
var imageData = $ctx.getImageData(0, 0, $c.width, $c.height); 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; let k = (px.y) * $c.width + px.x; k = 4 * k; } $ctx.putImageData(imageData, 0,0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
fillRect/concat
fillRect/join
1×1 Image Data
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 benchmark and analyze the different approaches. **Benchmark Overview** The benchmark is designed to measure the performance of filling rectangles on a 2D canvas element using JavaScript. The script creates an image data object, populates it with random pixel values, and then uses the `fillRect` method to draw on the canvas. There are three test cases: 1. `fillRect/concat`: This test case uses string concatenation to construct the `fillStyle` string. 2. `fillRect/join`: This test case uses the `join()` method to concatenate the color values in an array. 3. `1×1 Image Data`: This test case uses the `getImageData` and `putImageData` methods to manipulate the image data directly. **Options Compared** The benchmark compares three different approaches: * **String Concatenation**: Using string concatenation (`+`) to construct the `fillStyle` string. * **Array Joining**: Using the `join()` method to concatenate the color values in an array. * **Image Data Manipulation**: Using the `getImageData` and `putImageData` methods to manipulate the image data directly. **Pros and Cons** 1. **String Concatenation**: * Pros: Simple, widely supported, and easy to implement. * Cons: Can lead to slower performance due to string creation and concatenation. 2. **Array Joining**: * Pros: More efficient than string concatenation since it avoids creating temporary strings. * Cons: May be less readable or more complex for some developers. 3. **Image Data Manipulation**: * Pros: Can potentially offer better performance by avoiding unnecessary string creation and concatenation. * Cons: Requires a deeper understanding of image data manipulation, which might not be familiar to all developers. **Libraries Used** None are explicitly mentioned in the benchmark definition or test cases. However, it's likely that the `Canvas` API and related functions (e.g., `createImageData`, `fillRect`) are part of the standard JavaScript library. **Special JS Features/Syntax** The benchmark uses a few advanced features: * **Template literals** (in the script preparation code): Used to create a variable `$pxls` with an array of objects. * **Bit-shifting operators** (`<< 0`): Used in some calculations, likely for performance optimization. Overall, this benchmark helps identify performance differences between various approaches to filling rectangles on a canvas element, which can be useful for optimizing rendering code.
Related benchmarks:
Setting Canvas Pixel123
Setting Canvas Pixel 4
Setting Canvas Pixel with lots of iterations - looking more at get/put
Setting Canvas Pixel test
Setting Canvas Pixel actually working
Comments
Confirm delete:
Do you really want to delete benchmark?