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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of creating and reusing Canvas gradients in JavaScript. It explores three different approaches: **1. "Create":** This test case measures the time it takes to create a new `LinearGradient` object for each iteration of a loop (10 iterations). * **Pros:** Demonstrates the overhead of gradient creation. * **Cons:** Inefficient for repeated use, as gradients are created and discarded repeatedly. **2. "reuse":** This test case measures the time it takes to set an existing `LinearGradient` object (`grad`) as the fill style and draw a rectangle multiple times (10 iterations). * **Pros:** Efficient if the gradient remains consistent, avoiding repeated creation. * **Cons:** Doesn't consider scenarios where the gradient needs to be modified frequently. **3. "fill only":** This test case measures the time it takes to fill a rectangle using a pre-existing `LinearGradient` object (`grad`) but without creating new gradients in each iteration (10 iterations). * **Pros:** Focuses on the performance of drawing with an existing gradient. * **Cons:** May not capture the full picture if gradient creation is also a significant performance factor. **Alternatives:** * **Pre-generating Gradients:** If you know your gradients will be used repeatedly, create them outside the loop and reuse them throughout the code. This can eliminate the overhead of constant gradient creation. **Canvas & Context:** The benchmark utilizes the HTML5 Canvas API to draw shapes and apply gradients. * **Canvas:** A rectangular area on a web page where you can draw graphics using JavaScript. * **Context:** An object providing methods for drawing, filling, and manipulating content within a canvas element. This benchmark helps developers understand the performance implications of different approaches when working with Canvas gradients in JavaScript. The results can guide developers in choosing the most efficient method based on their specific use case.
Related benchmarks:
Canvas gradient perf
Canvas gradient perf
Canvas gradient perf
Canvas gradient perf
Comments
Confirm delete:
Do you really want to delete benchmark?