Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Canvas paths performance without alpha
(version: 0)
Comparing performance of:
Direct draw vs New path vs Cached path vs Direct with beginpath vs Direct fillrect
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<canvas align="center" id="canvas" width="800" height="800"></canvas>
Script Preparation code:
var ctx = document.getElementById('canvas').getContext('2d', {alpha: false}); ctx.fillStyle = 'rgba(0,1,0,1)'; var rects = [] for (var i = 0; i < 50; i++) { var width = 5 + i * 5; var height = 7 + i * 3; var path = new Path2D(); path.rect(0, 0, width, height); var rect = { x: i, y: i, width: width, height: height, cachedPath: path }; rects.push(rect); } ctx.clearRect(0, 0, 500, 500);
Tests:
Direct draw
for (var i = 0; i < rects.length; i++) { var rect = rects[i]; ctx.rect(rect.x, rect.y, rect.width, rect.height); ctx.fill(); }
New path
for (var i = 0; i < rects.length; i++) { var rect = rects[i]; var path = new Path2D(); path.rect(rect.x, rect.y, rect.width, rect.height) ctx.fill(path) }
Cached path
for (var i = 0; i < rects.length; i++) { var rect = rects[i]; ctx.translate(rect.x, rect.y); ctx.fill(rect.path); ctx.translate(-rect.x, -rect.y); }
Direct with beginpath
for (var i = 0; i < rects.length; i++) { var rect = rects[i]; ctx.beginPath(); ctx.rect(rect.x, rect.y, rect.width, rect.height); ctx.fill(); }
Direct fillrect
for (var i = 0; i < rects.length; i++) { var rect = rects[i]; ctx.fillRect(rect.x, rect.y, rect.width, rect.height); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Direct draw
New path
Cached path
Direct with beginpath
Direct fillrect
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):
I'll break down the benchmark definition and test cases, explaining what's being tested, the pros and cons of each approach, and other considerations. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark for testing the performance of different drawing methods on a 2D canvas. The main idea is to draw multiple rectangles with varying sizes and positions, using different approaches: 1. **Direct draw**: Drawing directly to the canvas without creating any intermediate path objects. 2. **New path**: Creating a new `Path2D` object for each rectangle and drawing it using the `fill()` method. 3. **Cached path**: Using the `translate()` and `fill()` methods to quickly render the rectangles without creating a new path object. 4. **Direct with beginpath**: Drawing directly to the canvas, but using the `beginPath()` method before drawing each rectangle. **Options Compared** These test cases compare the performance of different approaches in two aspects: * Execution speed: How fast can each approach draw all rectangles? * Memory usage: How much memory is allocated for each approach? **Pros and Cons of Each Approach** 1. **Direct draw**: * Pros: Fast execution, low memory usage. * Cons: Can be slow if many rectangles are drawn, as it requires multiple `ctx.rect()` calls. 2. **New path**: * Pros: Flexibility to easily modify or replace rectangle shapes, no need to worry about caching. * Cons: Slower execution due to creating a new `Path2D` object for each rectangle, higher memory usage. 3. **Cached path**: * Pros: Fast execution by leveraging the browser's cache and reuse of canvas rendering context. * Cons: Requires precise calculation of transformations to avoid drawing incorrect rectangles, potentially introduces aliasing issues. 4. **Direct with beginpath**: * Pros: Combines benefits of direct draw and cached path approaches, allows for better control over rectangle shapes. * Cons: Requires additional processing steps, may introduce overhead from `beginPath()` calls. **Other Considerations** * The benchmark uses a JavaScript engine (Firefox Mobile 48) running on Android, which might have specific optimizations or limitations that affect performance. * The canvas is rendered in a fixed size (800x800), but the rectangles are drawn at variable positions and sizes. This could lead to varying rendering times depending on the browser's optimization strategies. **Library and Special JS Features** The benchmark uses the `Path2D` class from the Web API, which is a part of the HTML5 standard. No special JavaScript features or syntax are required beyond ES6+ compatibility. **Alternatives** To further optimize drawing performance, you could consider: 1. Using WebGL or other GPU-accelerated rendering APIs for more efficient rendering. 2. Implementing custom rendering algorithms that exploit specific browser optimizations. 3. Utilizing libraries like Three.js or fabric.js, which provide optimized rendering and event handling mechanisms. Keep in mind that the choice of rendering method depends on the specific use case, performance requirements, and target audience.
Related benchmarks:
Canvas paths performance
Canvas paths performance -fixed
Canvas paths performance v94
compare drawing lots of sprites (filled circles and squares) using various canvas methods II WITH ROUNDED PIXELS
Comments
Confirm delete:
Do you really want to delete benchmark?