Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel 5
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data
Created:
3 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 d=$px.data; for (var i=500;i--;){ var px=$pxls[$i++ % 10000]; 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 dive into explaining the provided benchmark. **Benchmark Definition** The benchmark is designed to test the performance of drawing a single pixel on a canvas using different approaches. The script preparation code sets up a canvas element with a 2D drawing context, clears the canvas, and creates an image data object representing a single pixel. Then, it populates an array (`$pxls`) with random pixel color values. **Options Compared** The benchmark compares three different methods for drawing a single pixel: 1. **`fillRect/concat`**: This approach concatenates the individual RGB values to form a string, which is then used as the `fillStyle` property of the context. The pixel is drawn using `fillRect`. 2. **`fillRect/join`**: Similar to the previous method, but it uses an array with the concatenated RGB values instead of a string. 3. **`1×1 Image Data`**: This approach stores the pixel color data in an image object (`$px`) and then draws the entire image using `putImageData`. **Pros and Cons** * **`fillRect/concat`**: * Pros: Simple, easy to understand, and likely to be efficient. * Cons: May incur additional overhead due to string concatenation, which can lead to slower performance. * **`fillRect/join`**: * Pros: Similar benefits as `fillRect/concat`, but avoids the potential overhead of string concatenation. * Cons: Slightly more complex than `fillRect/concat`. * **`1×1 Image Data`**: * Pros: Can be efficient for large images, as it avoids unnecessary string manipulations and directly accesses pixel data. * Cons: Requires creating an additional image object, which can lead to overhead. **Library** There is no specific library used in this benchmark. It only relies on built-in JavaScript features, such as the `Canvas` API and `ImageData`. **Special JS Feature** This benchmark uses a feature called "string concatenation" (using the `+` operator or `join()` method), which can be slow due to the overhead of creating temporary strings. **Other Considerations** * **Browser Variability**: The benchmark is run on Chrome 108, but other browsers might have different performance characteristics. * **Canvas Optimization**: Modern browsers often include optimizations for canvas rendering, such as hardware acceleration. This could affect the results. * **JavaScript Engine**: Different JavaScript engines (e.g., V8 in Chrome) may optimize or execute code differently. **Alternatives** If you were to create a similar benchmark, you might consider adding other approaches, such as: * Using `ImageBitmap` API for more efficient image processing. * Employing SIMD instructions using WebAssembly. * Experimenting with different string manipulation techniques (e.g., using a buffer or a library like Stringify). * Adding support for older browsers or alternative rendering engines. Keep in mind that the choice of approaches will depend on your specific use case and requirements.
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
Comments
Confirm delete:
Do you really want to delete benchmark?