Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel with preformat
(version: 0)
Added additional test to test using preformatted strings vs concat vs join
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data vs fillrect/preformat
Created:
4 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); }
fillrect/preformat
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); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
fillRect/concat
fillRect/join
1×1 Image Data
fillrect/preformat
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):
**Benchmark Explanation** The provided benchmark measures the performance of four different approaches to draw a single pixel on a canvas element in JavaScript. **Approaches Compared** 1. **Preformatted string concatenation**: This approach uses string concatenation to build the `rgba` color string, e.g., `"rgba(${px.r}, ${px.g}, ${px.b}, ${(px.a / 255)}``. 2. **Array.join() method**: This approach uses the `join()` method to concatenate the array elements into a single string, e.g., `[px.r,px.g,px.b,px.a/255].join()`. 3. **Image Data manipulation**: This approach directly manipulates the image data using `putImageData`, e.g., `$ctx.putImageData($px, px.x, px.y)`. 4. **Template literal (preformatted string)**: This approach uses a template literal to build the `rgba` color string, e.g., `rgba(${px.r}, ${px.g}, ${px.b}, ${(px.a / 255)})`. **Pros and Cons of Each Approach** 1. **Preformatted string concatenation**: Pros: * Easy to read and maintain. * Fast performance due to direct string manipulation. Cons: * Less readable for complex color values. 2. **Array.join() method**: Pros: * More readable than preformatted string concatenation for complex color values. Cons: * Slower performance due to string creation and concatenation. 3. **Image Data manipulation**: Pros: * Fast performance due to native image data manipulation. Cons: * Less readable for JavaScript developers without image processing expertise. 4. **Template literal (preformatted string)**: Pros: * More readable than preformatted string concatenation for complex color values. Cons: * May have slower performance due to template literal compilation. **Library and Syntax** No external libraries are used in this benchmark, only native JavaScript features. **Special JS Feature or Syntax** None mentioned in the benchmark definition. **Other Alternatives** Alternative approaches to drawing a single pixel on a canvas element could include: 1. Using `CanvasRenderingContext2D.createImageData()` and `fillRect()`. 2. Using `WebGL` and `Buffer` APIs. 3. Using `Pixi.js` or other libraries optimized for 2D rendering. Note that these alternatives may have different performance characteristics, readability, and maintainability compared to the approaches tested in this benchmark.
Related benchmarks:
Setting Canvas Pixel with extra test case using backtick
Setting Canvas Pixel Concat/Join/template vs Image data
Setting Canvas Pixel test
Setting Canvas Pixel With Same Color
Comments
Confirm delete:
Do you really want to delete benchmark?