Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Canvas Pixel vs Fill rect 1px
(version: 0)
Comparing performance of:
fillRect/concat vs 1×1 Image Data
Created:
5 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(250, 100); $pxls = []; for (var i=0; i<25000; ++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=250;i--;){ for(var j=100;j--;){ var px = $pxls[i*j] $ctx.fillStyle = 'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')'; $ctx.fillRect(px.x, px.y, 1, 1); } }
1×1 Image Data
for (var i=0; i<25000; ++i){ var px=$pxls[i], d=$px.data[i]; d[0] = px.r; d[1] = px.g; d[2] = px.b; d[3] = px.a; } $ctx.putImageData($px, 500,500);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
fillRect/concat
1×1 Image Data
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
fillRect/concat
77.4 Ops/sec
1×1 Image Data
81.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The provided benchmark compares two approaches to drawing 25000 pixel values on a canvas: `fillRect/concat` and using image data (`1×1 Image Data`). Both methods are used to draw pixels at specific coordinates with varying red, green, blue (RGB), and alpha (RGBA) values. **Test Case Analysis** ### 1. "Canvas Pixel vs Fill rect 1px" This test case is comparing the performance of two approaches: a. `fillRect/concat`: This approach uses the `fillStyle` property to set the fill color for each pixel, followed by a call to `fillRect()` with the x, y, width, and height of the pixel. b. Using image data (`1×1 Image Data`): In this approach, an image is created using `createImageData()`, where each pixel's color value is stored in an array. The image data is then used with `putImageData()` to draw the pixels on the canvas. **Pros and Cons of Each Approach** **a. `fillRect/concat`:** Pros: * Simple and straightforward implementation * Easy to understand for beginners Cons: * Potential performance issues due to repeated calls to `fillStyle` and `fillRect()` * Can be slower for large numbers of pixels **b. Using image data (`1×1 Image Data`):** Pros: * More efficient, as the browser can use hardware acceleration * Less repeated function calls, reducing overhead Cons: * Requires more complex implementation, including creating an image and accessing its pixel data * May require additional memory allocation for the image data array **Other Considerations** * Both approaches are using JavaScript to interact with the canvas element. The choice of approach may depend on the specific requirements of the application. * Using image data can be beneficial for performance-critical applications, as it allows the browser to use hardware acceleration. **Library and Special JS Features Used** * No libraries are explicitly mentioned in the benchmark definition or test cases. * There is no use of special JavaScript features like Web Workers, async/await, or Promises. However, some browsers may optimize certain operations for Chrome (e.g., `putImageData()`). **Alternatives to the Benchmark** Other alternatives to compare these two approaches could include: * Using a graphics API like WebGL * Implementing a custom pixel shader using shaders * Comparing performance using other graphics libraries or frameworks However, the provided benchmark is a good starting point for comparing the performance of these two approaches in JavaScript.
Related benchmarks:
Setting Canvas Pixel
Setting Canvas Pixel
Setting Canvas Pixel
Canvas Pixel vs Fill rect
Comments
Confirm delete:
Do you really want to delete benchmark?