Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Canvas fill test
(version: 2)
Comparing performance of:
putImageData vs fillRect
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<canvas id="c" width="800" height="300"></canvas>
Script Preparation code:
$canvas = document.getElementById('c'); $ctx = $canvas.getContext('2d'); $cw = $canvas.width; $ch = $canvas.height; $id = $ctx.getImageData(0, 0, 1, 1); $ctx.clearRect(0, 0, $cw, $ch); function rng() { return [ Math.floor(Math.random() * $cw), Math.floor(Math.random() * $ch), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256) ]; }
Tests:
putImageData
{ const [x, y, r, g, b] = rng(); $id.data[0] = r; $id.data[1] = g; $id.data[2] = b; $ctx.putImageData($id, x, y); }
fillRect
{ const [x, y, r, g, b] = rng(); $ctx.fillStyle = `rgb(${r},${g},${b})`; $ctx.fillRect(x, y, 1, 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
putImageData
fillRect
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 provided benchmark and its components to provide a clear explanation. **Benchmark Overview** MeasureThat.net is a platform for creating and running JavaScript microbenchmarks. The provided benchmark is a Canvas fill test, which aims to measure the performance of two different approaches: using `putImageData` and `fillRect`. **Script Preparation Code** The script preparation code sets up the necessary variables and canvas context: ```javascript $canvas = document.getElementById('c'); $ctx = $canvas.getContext('2d'); $cw = $canvas.width; $ch = $canvas.height; $id = $ctx.getImageData(0, 0, 1, 1); $ctx.clearRect(0, 0, $cw, $ch); function rng() { return [ Math.floor(Math.random() * $cw), Math.floor(Math.random() * $ch), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256) ]; } ``` The `rng()` function generates random values for the x, y coordinates and RGB color components. **Html Preparation Code** The HTML preparation code sets up a canvas element with an ID "c" and specified width and height: ```html <canvas id="c" width="800" height="300"></canvas> ``` **Individual Test Cases** There are two individual test cases: 1. **putImageData** ```javascript const [x, y, r, g, b] = rng(); $id.data[0] = r; $id.data[1] = g; $id.data[2] = b; $ctx.putImageData($id, x, y); ``` This test case uses `putImageData` to draw a small rectangle at the generated random coordinates with RGB color components. 2. **fillRect** ```javascript const [x, y, r, g, b] = rng(); $ctx.fillStyle = `rgb(${r},${g},${b})`; $ctx.fillRect(x, y, 1, 1); ``` This test case uses `fillRect` to draw a small rectangle at the generated random coordinates with RGB color components. **Library: `getImageData`** The `getImageData()` method is used to create a 1x1 pixel image data object, which is then used in both test cases. This library function provides a way to access individual pixels within an image and manipulate their properties. **Special JS Feature/Syntax: Random number generation** Both test cases use the `rng()` function to generate random values for x, y coordinates and RGB color components. No special JavaScript feature or syntax is used in this benchmark. **Other Alternatives** If you wanted to measure performance using different approaches, here are some alternatives: * **Using WebGL**: Instead of using 2D canvas, you could use WebGL to draw the rectangle. * **Using a library like Pixi.js**: You could use a framework like Pixi.js to handle drawing and rendering. * **Measuring CPU-bound operations**: If you wanted to measure CPU performance instead of graphics rendering, you could replace the graphics-related code with CPU-intensive operations. Please note that these alternatives would require significant changes to the benchmark and may not be directly comparable to the original implementation.
Related benchmarks:
Setting Canvas Pixel
Setting Canvas Pixel With Same Color
Setting multiple canvas pixels
Setting multiple canvas pixels
Comments
Confirm delete:
Do you really want to delete benchmark?