Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Setting Canvas Pixel (Larger Test Size)
(version: 0)
Comparing performance of:
fillRect/concat vs fillRect/join vs 1×1 Image Data
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=250000;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=250000;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=250000;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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
fillRect/concat
2.2 Ops/sec
fillRect/join
2.2 Ops/sec
1×1 Image Data
0.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is an essential task for any developer, and MeasuringThat.net is a great platform to do so. **Benchmark Overview** The provided benchmark tests the rendering of 10,000 small images on a canvas element using three different approaches: `fillRect` with concatenated string values, `fillRect` with array values, and using `putImageData`. The goal is to determine which approach is the most efficient in terms of performance. **Options Compared** The three options compared are: 1. **`fillRect/concat`**: This option uses a concatenated string to construct the `rgba` color value for each pixel. This involves concatenating four values: red, green, blue, and alpha. 2. **`fillRect/join`**: This option uses an array to store the color values and then joins them using the `join()` method to create the `rgba` string. 3. **`1×1 Image Data`**: This option uses a 1x1 image data object to render each pixel, which is expected to be more efficient than the other two options. **Pros and Cons of Each Approach** 1. **`fillRect/concat`**: * Pros: Simple and straightforward. * Cons: Concatenating strings can lead to slower performance due to string creation and concatenation overhead. 2. **`fillRect/join`**: * Pros: More efficient than `concat`, as it avoids creating temporary strings. * Cons: Requires an array to store the color values, which may not be desirable for large datasets. 3. **`1×1 Image Data`**: * Pros: Expected to be the most efficient approach, as it uses a pre-allocated image data object to render each pixel. * Cons: May require more memory allocation and deallocation, depending on the browser's implementation. **Library Used** None of the provided benchmark definitions explicitly use any external libraries. However, they do rely on the HTML5 canvas element and its APIs. **Special JS Feature or Syntax** The `join()` method is used in the `fillRect/join` option to concatenate array elements into a string. This is a built-in JavaScript method that joins an array of values with a specified separator (in this case, no separator). **Other Considerations** When measuring performance in JavaScript, it's essential to consider factors like: * Browser versions and engine optimizations * Canvas element size and resolution * Pixel density and color depth * Memory allocation and deallocation Keep in mind that these factors can significantly impact the benchmark results. MeasuringThat.net provides a controlled environment for comparing different approaches, but real-world scenarios may introduce additional variables to consider. **Alternatives** If you're interested in exploring alternative performance optimization techniques or testing other JavaScript benchmarks, here are some suggestions: 1. **JSPerf**: A popular online tool for measuring JavaScript performance. 2. **Benchmark.js**: A library for creating and running JavaScript benchmarks. 3. **WebAssembly Performance**: Measuring the performance of WebAssembly-enabled browsers. These alternatives can help you explore different aspects of JavaScript performance optimization or test other scenarios that may not be covered by MeasuringThat.net.
Related benchmarks:
Setting Canvas Pixel
Setting Canvas Pixel123
Setting Canvas Pixel 4
Setting Canvas Pixel test
Setting Canvas Pixel actually working
Comments
Confirm delete:
Do you really want to delete benchmark?