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; $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):
I'll break down the benchmark and its options for you. **Benchmark Overview** The benchmark measures the performance of different approaches to drawing a single pixel on a 2D canvas using JavaScript. The test case generates an array of 10,000 pixels with random red, green, blue, and alpha values, and then attempts to draw each pixel using three different methods: 1. Directly accessing the `fillStyle` property and setting its value manually. 2. Using string concatenation to construct a CSS-style color string. 3. Using `putImageData` method. **Options Compared** The benchmark compares the performance of these three approaches: * **Direct access**: Setting `fillStyle` directly using an object literal (`{ r: px.r, g: px.g, b: px.b, a: px.a }`). This approach is likely to be the fastest, as it involves a simple assignment operation. * **String concatenation**: Using string concatenation to construct a CSS-style color string (`'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')'`). This approach involves creating strings and concatenating them, which can be slower than direct assignment. * **ImageData**: Using `putImageData` method to draw the pixel. This approach involves creating an image data object from the pixel array (`$px`) and then drawing it onto the canvas. **Pros and Cons of Each Approach** * **Direct access**: Pros: Fastest, most efficient. Cons: Requires direct access to the `fillStyle` property, which can be brittle and prone to errors. * **String concatenation**: Pros: Easy to read and write, less error-prone than direct assignment. Cons: Slower due to string creation and concatenation overhead. * **ImageData**: Pros: Can handle more complex drawing operations, such as scaling or rotation. Cons: Slower due to the need to create an image data object and perform additional operations. **Library and Special JS Features** None of these approaches rely on any external libraries or special JavaScript features beyond standard ECMAScript syntax. **Other Alternatives** If you were to optimize this benchmark further, you might consider exploring alternative drawing methods, such as: * Using a hardware-accelerated canvas (e.g., WebGL) for more complex graphics operations. * Using a Just-In-Time (JIT) compiler or other optimization techniques to improve performance. * Using parallel processing or multi-threading to take advantage of multiple CPU cores. Keep in mind that these alternatives may introduce additional complexity and dependencies, which could impact the overall benchmark's accuracy and comparability.
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?