Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel with Template String
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/join vs fillRect/template-string
Created:
2 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); }
fillRect/template-string
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 (3)
Previous results
Fork
Test case name
Result
fillRect/concat
fillRect/join
fillRect/template-string
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 Overview** The benchmark is testing how to set pixel values on a canvas element using different approaches. The test data is generated by creating an array of 10,000 pixels with random red, green, blue, and alpha channel values. The goal is to fill each pixel on the canvas with these values at a rate of 500 iterations per second. **Options Being Compared** There are three options being compared: 1. **Template String** ( benchmark definition: "for (var i=500;i--;){\r\nvar px = $pxls[$i++ % 10000];\r\n$ctx.fillStyle = `rgba(${px.r},${px.g},${px.b},${px.a/255})`;\r\n$ctx.fillRect(px.x, px.y, 1, 1);\r\n}" ) 2. **Concatenation** ( benchmark definition: "for (var i=500;i--;){\r\nvar px = $pxls[$i++ % 10000];\r\n$ctx.fillStyle = 'rgba(' + [px.r,px.g,px.b,px.a/255].join() + ')';\r\n$ctx.fillRect(px.x, px.y, 1, 1);\r\n}" ) 3. **Template String with ES6 Template Literal** ( benchmark definition: "for (var i=500;i--;){\r\nvar px = $pxls[$i++ % 10000];\r\n$ctx.fillStyle = `rgba(${px.r},${px.g},${px.b},${px.a/255})`;\r\n$ctx.fillRect(px.x, px.y, 1, 1);\r\n}" ) **Pros and Cons of Each Approach** * **Template String**: This approach is simple and easy to read. It uses the `$` symbol to reference variables in the template string. The only potential issue is that it may not be as efficient as other approaches due to the overhead of concatenating strings. * **Concatenation**: This approach uses array methods to concatenate values into a string, which can be more efficient than using template strings with variables. However, it's also slightly more verbose and error-prone. * **Template String with ES6 Template Literal**: This approach is similar to the first one but uses an ES6 template literal instead of concatenating strings. It's likely to be faster and more efficient than the template string approach. **Library Used** There is no explicit library mentioned in the benchmark, but it appears that the `createImageData` function and the `getContext` method are part of the HTML5 Canvas API. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. It's purely functional programming using ES6 syntax. **Other Considerations** The benchmark is likely designed to measure the performance difference between these three approaches on a desktop system running Chrome 108. The results will give insight into which approach is fastest and most efficient for this specific use case. **Alternative Approaches** Some alternative approaches that could be tested in this benchmark include: * Using a different data structure, such as an object or a struct * Using a different rendering algorithm, such as pixel batching or instancing * Using a different language or framework, such as C++ or Node.js These alternatives could provide interesting insights into the performance characteristics of each approach and help to identify potential bottlenecks in the benchmark.
Related benchmarks:
Setting Canvas Pixel
Setting Canvas Pixel123
Setting Canvas Pixel with lots of iterations - looking more at get/put
Setting Canvas Pixel actually working
Comments
Confirm delete:
Do you really want to delete benchmark?