Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel
(version: 1)
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data
Created:
8 years ago
by:
Registered User
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:
Run details:
(Test run date:
6 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
fillRect/concat
1846.7 Ops/sec
fillRect/join
1610.0 Ops/sec
1×1 Image Data
91.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Overview** The benchmark measures the performance of different approaches for drawing a single pixel on a 2D canvas using JavaScript. The test case uses a predefined set of 10,000 pixels with random red, green, blue, and alpha values. The benchmark compares three different methods: 1. `fillRect/concat` 2. `fillRect/join` 3. `1×1 Image Data` **Options Comparison** ### `fillRect/concat` This method uses string concatenation to build the `rgba` color string. ```javascript $ctx.fillStyle = 'rgba(' + px.r + ',' + px.g + ',' + px.b + ',' + (px.a / 255) + ')'; ``` Pros: * Simple and concise code * Easy to read and maintain Cons: * String concatenation can lead to performance issues due to repeated creation of new strings * May not be the most efficient way to build a string in JavaScript ### `fillRect/join` This method uses the `join()` method to concatenate the color components into a single string. ```javascript $ctx.fillStyle = 'rgba(' + [px.r, px.g, px.b, px.a/255].join() + ')'; ``` Pros: * More efficient than concatenation due to reducing string creation overhead * Still readable and maintainable Cons: None notable. ### `1×1 Image Data` This method uses the `putImageData` function to draw a single pixel from an image data array. ```javascript $ctx.putImageData($px, px.x, px.y); ``` Pros: * May be more efficient due to using native hardware acceleration for image rendering Cons: * Requires additional memory allocation and management for the image data array * May require more complex code compared to the other two methods **Library Used** None. **Special JS Feature or Syntax** The benchmark uses the following features: * ES6 string interpolation (`px.r` is interpolated into a string) * Array destructuring (not necessary in this case, but used for demonstration purposes) However, the `putImageData` function is not a new feature introduced in modern JavaScript versions. It's an older API that was introduced in HTML 5. **Other Alternatives** If you wanted to test alternative methods, you could consider: * Using a library like Pixi.js or Fabric.js for rendering graphics * Implementing custom drawing functions using WebAssembly or WebGL * Using a different rendering approach, such as vector graphics Keep in mind that these alternatives may introduce additional complexity and dependencies compared to the original benchmark. **Benchmark Preparation Code Explanation** The script preparation code sets up a canvas element with an ID of "c" and creates a 2D drawing context (`$ctx`) for it. It then clears the canvas, creates a new image data object (`$px`), and populates it with random pixel values (`$pxls`). The script loop increments through the array of pixels, drawing each one on the canvas using the specified method. **Html Preparation Code Explanation** The HTML preparation code defines an empty `<canvas>` element with an ID of "c" and sets its width to 800 and height to 300.
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?