Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Canvas gradient perf
(version: 0)
Comparing performance of:
Create vs reuse vs fill only
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<canvas id="canvas1" width="512" height="512"></canvas> <style> canvas { border: 1px solid black; } </style> <script> var canvas = document.getElementById('canvas1'); var ctx = canvas.getContext('2d'); var grad = ctx.createLinearGradient(0, 0, 4, 256); grad.addColorStop(0, 'red'); grad.addColorStop(0.5, 'rgba(0,0,255,0.5)'); grad.addColorStop(1, 'lime'); </script>
Tests:
Create
for (var i = 0; i < 10; i++) { var grad2 = ctx.createLinearGradient(0, 0, 4, 256); grad2.addColorStop(0, 'red'); grad2.addColorStop(0.5, 'rgba(0,0,255,0.5)'); grad2.addColorStop(1, 'lime'); ctx.fillStyle = grad2; ctx.fillRect(0,0,512,512); }
reuse
for (var i = 0; i < 10; i++) { ctx.fillStyle = grad; ctx.fillRect(0,0,512,512); }
fill only
ctx.fillStyle = grad; for (var i = 0; i < 10; i++) { ctx.fillRect(0,0,512,512); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Create
reuse
fill only
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.1:latest
, generated one year ago):
Let's break down the benchmark definition and test cases. **Benchmark Definition** The benchmark is called "Canvas gradient perf" (performance). It involves creating a linear gradient on a canvas element using the `createLinearGradient` method of the 2D drawing context (`ctx`). The gradient has three color stops: red at the start, a semi-transparent blue in the middle, and lime at the end. **Test Cases** There are three test cases: 1. **Create**: This test creates a new linear gradient on each iteration of the loop (10 times). On each iteration, it creates a new gradient using `ctx.createLinearGradient(0, 0, 4, 256)`, adds color stops to it, and then uses this gradient to fill a rectangle (`ctx.fillRect(0,0,512,512)`). 2. **Reuse**: This test reuses the same linear gradient created in the first test case (using `grad` variable). On each iteration of the loop, it simply assigns the reused gradient to `ctx.fillStyle` and uses this gradient to fill a rectangle. 3. **Fill only**: This test is similar to "Reuse", but it assigns the reused gradient to `ctx.fillStyle` before entering the loop. This means that the gradient is created once (before the loop) and reused throughout the test. **Latest Benchmark Result** The latest benchmark result shows the performance of each test case on a Chrome 99 browser running on a desktop Mac OS X 10.15.7 system. The results are in executions per second (EPS). **Analysis** Here's what we can infer from these tests: * Creating a new linear gradient on each iteration is expensive (`Create` test). * Reusing the same gradient throughout the test is significantly faster than creating a new one (`Reuse` and `Fill only` tests). * Assigning the reused gradient to `ctx.fillStyle` before entering the loop (as in `Fill only`) doesn't provide any additional performance benefits compared to reusing it within the loop (as in `Reuse`). **Library and Special JS Feature** No special JavaScript features or libraries are used in these tests. The canvas element is a standard HTML feature, and the 2D drawing context (`ctx`) is used directly without any third-party library. **Other Considerations and Alternatives** Some potential considerations: * Cache invalidation: if the gradient is recreated on each iteration, the cache might be invalidated, leading to performance degradation. * Gradient complexity: if more complex gradients are involved (e.g., radial or conic), the performance difference between creating a new one vs. reusing it might change. Alternative approaches could include: * Using a caching mechanism to store the created gradient and reuse it throughout the test. * Pre-creating multiple gradients with different properties and switching between them as needed. * Exploring other rendering options, such as using WebGL or a dedicated graphics library.
Related benchmarks:
Canvas gradient perf
Canvas gradient perf
Canvas gradient perf
Canvas gradient perf
Comments
Confirm delete:
Do you really want to delete benchmark?