Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel123
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data
Created:
6 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})`; $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):
Measuring JavaScript performance is a complex task, and MeasuredThat.net provides a great platform for benchmarking different approaches. **Benchmark Definition** The provided benchmark definition involves creating an image data buffer and then drawing pixels on a canvas using various methods. The script preparation code sets up the necessary variables and context, while the HTML preparation code defines the canvas element with the required width and height. Here's a breakdown of what's being tested: * Creating an image data buffer (`$pxls`) with 10,000 entries. * Drawing pixels on the canvas using different methods: + `fillRect/concat`: Directly concatenates color values to create the fill style string. + `fillRect/join`: Joins color values using a space separator. + `1×1 Image Data`: Uses the `putImageData` method to draw the image data directly on the canvas. **Options Compared** The three options being compared are: 1. **Direct concatenation**: This approach uses string concatenation (`"rgba( ${px.r}, ${px.g}, ${px.b})"` ) to create the fill style string. 2. **Joining color values**: This approach uses an array of color values and joins them using a space separator (`"[px.r,px.g,px.b,px.a/255].join()"`) to create the fill style string. 3. **Using `putImageData`**: This approach uses the `putImageData` method to draw the image data directly on the canvas. **Pros and Cons of Each Approach** Here's a brief summary: 1. **Direct Concatenation**: * Pros: Simple, easy to read, and maintain. * Cons: Potential performance issues due to string concatenation overhead. 2. **Joining Color Values**: * Pros: Efficient use of array operations and reduced string overhead. * Cons: More complex code structure, potentially harder to read and debug. 3. **Using `putImageData`**: * Pros: Most efficient approach for drawing small images, reduces overhead of string manipulation. * Cons: Requires more memory allocation and management, as it needs to store the image data. **Library Used** The benchmark uses the HTML5 Canvas API, which is a built-in library in most modern browsers. The `putImageData` method is a part of this API. **Special JavaScript Feature/ Syntax** This benchmark does not use any special JavaScript features or syntax beyond standard ES6 features. **Other Alternatives** If you want to explore alternative approaches for similar benchmarks, consider the following: * Using WebGL (Web Graphics Library) instead of Canvas API. * Optimizing the image data storage and manipulation using WebAssembly (WASM). * Implementing custom rendering loops using Web Workers or native code. Keep in mind that each approach has its strengths and weaknesses, and choosing the best one depends on the specific requirements and constraints of your project.
Related benchmarks:
Setting Canvas Pixel 4
Setting Canvas Pixel 3
Setting Canvas Pixel test
Setting Canvas Pixel actually working
Comments
Confirm delete:
Do you really want to delete benchmark?