Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data
Created:
8 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
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);
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 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: 1. **Setting Canvas Pixel**: This test case creates a canvas element, gets its 2D drawing context, clears the canvas, creates an image data object with a single pixel, populates it with random color values, and then uses these values to fill a rectangle on the canvas. 2. **fillRect/concat**: This test case is similar to the first one but uses string concatenation to build the RGBA string for the `fillStyle` property instead of joining an array. 3. **1×1 Image Data**: This test case creates an image data object with a single pixel, populates it with random color values, and then applies this image data to a specific position on the canvas. **Comparison Options** The three test cases differ in how they manipulate the RGBA value for the `fillStyle` property: * **fillRect/concat**: Uses string concatenation (`'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')'`) to build the RGBA string. * **fillRect/join**: Uses array joining (`[px.r,px.g,px.b,px.a/255].join()`) to build the RGBA string. * **1×1 Image Data**: Creates an image data object with individual pixel values and applies it to the canvas. **Pros and Cons of each approach** * **fillRect/concat**: This approach is more verbose but can be faster due to the optimized string concatenation operation in modern JavaScript engines. However, it may have a slight performance overhead due to the additional function calls. * **fillRect/join**: This approach is more concise but might incur a small performance penalty due to the array joining operation. * **1×1 Image Data**: This approach is the most efficient as it avoids unnecessary string manipulation and uses native image data operations. However, it requires creating an additional image data object. **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: * Using a custom pixel shader: This would allow for more complex color manipulation but might introduce significant performance overhead. * Using WebAssembly (WASM): This would enable running the benchmark in a WASM virtual machine, which could provide improved isolation and security benefits. However, it might incur additional compilation and runtime overhead. 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.
Related benchmarks:
Setting Canvas Pixel
Setting Canvas Pixel123
Setting Canvas Pixel 4
Setting Canvas Pixel with lots of iterations - looking more at get/put
Setting Canvas Pixel test
Comments
Confirm delete:
Do you really want to delete benchmark?