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="800" height="300"></canvas>
Script Preparation code:
$c = document.getElementById('c'); $ctx = $c.getContext('2d'); $ctx.clearRect(0, 0, 800, 300); $px = $ctx.createImageData(800, 300); $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:
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 dive into the benchmark and explain what's being tested. **Benchmark Purpose** The test is designed to measure the performance of different ways to draw pixels on a canvas using JavaScript. Specifically, it compares three approaches: 1. Using `fillStyle` with concatenated strings (`fillRect/concat`) 2. Using `fillStyle` with an array expression (`fillRect/join`) 3. Using `putImageData` with an image data array (`1×1 Image Data`) **Options Comparison** The benchmark tests these three options because they are common ways to draw pixels on a canvas using JavaScript. Here's a brief analysis of each option: * **fillRect/concat**: This method uses string concatenation to build the `fillStyle` string, which can lead to slower performance due to the overhead of creating and manipulating strings. * **fillRect/join**: This method uses an array expression to build the `fillStyle` string, which is more efficient than concatenation because it avoids creating intermediate strings. However, it may still have some overhead due to the creation and manipulation of arrays. * **1×1 Image Data**: This method uses `putImageData` to draw a single pixel at a time. It's likely the fastest option because it avoids any overhead related to string manipulation or array creation. **Pros and Cons** Here are some pros and cons of each approach: * **fillRect/concat**: Pros: simple to implement, easy to understand. Cons: may be slower due to string concatenation. * **fillRect/join**: Pros: more efficient than concatenation, but still has some overhead due to array creation. Cons: can be less intuitive for beginners. * **1×1 Image Data**: Pros: likely the fastest option, avoids any overhead related to string manipulation or array creation. Cons: may require more complex implementation. **Library Usage** In this benchmark, no libraries are explicitly mentioned as being used. However, it's worth noting that `putImageData` is a standard JavaScript method for drawing images on a canvas, so it doesn't introduce any external dependencies. **Special JS Features/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. The code uses standard JavaScript syntax and features, such as string concatenation, array expressions, and `putImageData`. **Alternatives** If you're looking for alternatives to these approaches, here are a few options: * Instead of using `fillRect/concat`, you could use the `createCanvas` method provided by modern browsers (e.g., `const canvas = document.createElement('canvas');`) or a library like CanvasJS. * For `fillRect/join`, you could consider using a faster string manipulation method, such as using a `StringBuilder` class (available in some JavaScript engines). * For `1×1 Image Data`, you could use other methods for drawing pixels on a canvas, such as using the `createPixelArray` method or a library like Pixi.js. Overall, this benchmark provides a useful comparison of different approaches to drawing pixels on a canvas using JavaScript. By understanding the pros and cons of each option, developers can choose the fastest and most efficient approach for their specific use case.
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?