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="300" 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=5000;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:
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 break down the provided JSON and benchmark. **Benchmark Definition** The benchmark is defined by two pieces of code: `Script Preparation Code` and `Html Preparation Code`. The first one sets up the canvas element, gets its 2D drawing context, clears it, creates an image data object, and populates it with random pixel values. The second one defines the HTML structure for the canvas element. **Options being compared** There are three individual test cases being compared: 1. `fillRect/concat`: This option uses a comma-separated string to pass color values to `setFillStyle()`. 2. `fillRect/join`: This option uses an array to pass color values to `setFillStyle()`. 3. `1×1 Image Data`: This option uses the `putImageData()` method to fill the canvas with image data. **Pros and Cons of each approach** * `fillRect/concat`: + Pros: Simple, easy to implement. + Cons: Might be slower due to string concatenation overhead. * `fillRect/join`: + Pros: Faster than concatenating strings, as it avoids unnecessary copies. + Cons: Requires creating an array and joining its elements, which might incur additional overhead. * `1×1 Image Data`: + Pros: Directly operates on the image data buffer, potentially faster. + Cons: Requires setting up a separate image data object, which can be slower. **Library usage** The benchmark uses the `OHRP/51.0.2830.40` browser, which is an older version of Opera. It's likely that this browser has specific optimizations or bugs that affect the performance of these test cases. **JavaScript features and syntax** There are no special JavaScript features or syntax being used in these benchmark definitions. They seem to be standard ECMAScript 5/6 code. **Other alternatives** Other ways to fill a canvas element might include: * Using `fillRect()` with multiple arguments (e.g., `fillRect(x, y, width, height)`). * Using a library like CanvasJS or Chart.js for rendering graphics. * Using WebGL or WebAssembly for more complex graphics rendering. Keep in mind that these alternatives may have different performance characteristics and might not be as straightforward to implement as the benchmark's code.
Related benchmarks:
Setting Canvas Pixel123
Setting Canvas Pixel 4
Setting Canvas Pixel with lots of iterations - looking more at get/put
Setting Canvas Pixel 3
Setting Canvas Pixel test
Comments
Confirm delete:
Do you really want to delete benchmark?