Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Canvas cache or Path 2D (updated)
(version: 0)
Comparing performance of:
Cached canvas vs Cached Path2D
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<canvas height="500" width="500"/> <canvas height="500" width="500"/>
Script Preparation code:
var [canvas1, canvas2] = Array.from(document.querySelectorAll('canvas')); var ctx1 = canvas1.getContext('2d') var ctx2 = canvas2.getContext('2d') var rects = new Array(100).fill(null).map(() => ({ x: Math.floor(Math.random() * 100), y: Math.floor(Math.random() * 100), width: Math.floor(Math.random() * 100) + 1, height: Math.floor(Math.random() * 100) + 1 }))
Tests:
Cached canvas
for (var rect of rects) { var cached = document.createElement('canvas') cached.width = rect.width cached.height = rect.height rect.cached = cached.getContext('2d') rect.cached.fillStyle = 'blue' rect.cached.fillRect(0, 0, rect.width, rect.height) ctx1.drawImage(rect.cached.canvas, rect.x, rect.y, rect.width, rect.height) } ctx1.clearRect(0, 0, 500, 500) for (var rect of rects) { ctx1.drawImage(rect.cached.canvas, rect.x, rect.y, rect.width, rect.height) }
Cached Path2D
for (var rect of rects) { rect.path = new Path2D() rect.path.rect(rect.x, rect.y, rect.width, rect.height) ctx2.fillStyle = rect.fill ctx2.fill(rect.path) } ctx2.clearRect(0, 0, 500, 500) for (var rect of rects) { ctx2.fillStyle = 'red' ctx2.fill(rect.path) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Cached canvas
Cached Path2D
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Goanna/6.7 Firefox/115.0 PaleMoon/33.7.2
Browser/OS:
Pale Moon (Firefox Variant) 33 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Cached canvas
69.4 Ops/sec
Cached Path2D
822.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Test** The benchmark tests two different approaches to rendering rectangles on a 2D canvas: 1. **Cached Canvas**: This approach creates a new `canvas` element for each rectangle, stores its context, and then uses it to draw the rectangle. 2. **Cached Path2D**: This approach uses the `Path2D` API to render the rectangles. **Comparison of Approaches** The benchmark compares the performance of these two approaches. * **Pros of Cached Canvas**: + Easier to implement, as it leverages existing canvas functionality. + May be more familiar to developers who are already comfortable with canvas. * Cons of Cached Canvas: + Creates a new `canvas` element for each rectangle, which can lead to increased memory usage and slower performance due to the overhead of creating multiple elements. + Requires additional code to store and manage the contexts. * **Pros of Cached Path2D**: + More efficient than cached canvas, as it avoids creating multiple `canvas` elements and uses a more optimized rendering path (Path2D). + May be faster due to reduced memory allocation and garbage collection. * Cons of Cached Path2D: + Requires support for the `Path2D` API, which may not be widely supported or familiar to developers. **Library: Path2D** The `Path2D` library is a 2D path manipulation API that allows you to create and manipulate paths on a canvas. It's designed to be more efficient than traditional canvas rendering methods and provides a range of features for creating complex shapes. In this benchmark, the `Path2D` library is used to render rectangles by creating a path for each rectangle and then using the `fill()` method to draw it. **Special JavaScript Feature: Async/Await** The benchmark uses async/await syntax in some test cases, which allows the code to wait for asynchronous operations (like drawing images) without blocking the execution of other parts of the code. This can make the code easier to read and maintain. However, since there is no mention of async/await being used outside of test cases, it's likely that this feature is only relevant to the specific implementation details of the benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Canvas Sprite Sheet**: Instead of creating multiple `canvas` elements, you could create a single sprite sheet and draw each rectangle from it. This can reduce memory usage but may increase rendering time due to the need to calculate coordinates. * **WebGL**: You could use WebGL to render the rectangles, which provides even better performance than canvas or Path2D. However, this would require more complex setup and expertise with WebGL. Keep in mind that these alternatives might not be directly comparable to the cached canvas and Path2D approaches tested in this benchmark, as they require different implementation details and assumptions about the rendering pipeline.
Related benchmarks:
Canvas paths performance without alpha
Canvas paths performance -fixed
Canvas paths performance v94
Canvas cache or Path 2D (updated++)
Comments
Confirm delete:
Do you really want to delete benchmark?