Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test save/restore
(version: 0)
Comparing performance of:
resetTransform vs Save/restore
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<canvas id="canvas" width="800" height="300"></canvas>
Script Preparation code:
$canvas = document.getElementById('canvas'); $context = $canvas.getContext('2d'); $context.clearRect(0, 0, 800, 300); $iterations = 500;
Tests:
resetTransform
for (let i=0; i<$iterations; ++i) { $context.fillStyle = 'rgba(100,150,200,0.9)'; $context.fillRect(0, 0, 1, 1); $context.translate(50, 50); $context.rotate(50 * Math.PI / 180); $context.resetTransform(); }
Save/restore
for (let i=0; i<$iterations; ++i) { $context.save(); $context.fillStyle = 'rgba(100,150,200,0.9)'; $context.fillRect(0, 0, 1, 1); $context.translate(50, 50); $context.rotate(50 * Math.PI / 180); $context.restore(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
resetTransform
Save/restore
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/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
resetTransform
1026.8 Ops/sec
Save/restore
827.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Overview** The benchmark tests two approaches to drawing on a canvas element in JavaScript: one using `resetTransform()` (option 1) and the other using `save()` and `restore()` (option 2). **Option 1: Using `resetTransform()`** This option uses `resetTransform()` to reset the transformation matrix after each draw operation. The transformation matrix is used to perform transformations like rotation, scaling, and translation on the canvas. Pros: * Simpler code: Only one method call (`resetTransform()`) is needed to restore the default transformation state. * Faster execution: Since only a single method call is made, this option may be faster in terms of execution time. Cons: * Inaccurate results: If the canvas context is updated before calling `resetTransform()`, it will affect the subsequent draw operations. This might lead to inaccurate results or artifacts on the canvas. * Not suitable for complex graphics: Since only the transformation matrix is restored, this option may not be suitable for more complex graphics where multiple transformations are applied. **Option 2: Using `save()` and `restore()`** This option uses `save()` to save the current state of the transformation matrix and `restore()` to restore it after each draw operation. This approach allows subsequent draw operations to build upon the previous state without affecting each other. Pros: * Accurate results: By saving and restoring the transformation matrix, this option ensures accurate results and prevents artifacts on the canvas. * Suitable for complex graphics: This option is suitable for more complex graphics where multiple transformations are applied. Cons: * More code: Two method calls (`save()` and `restore()`) are needed to manage the transformation state, which may make the code slightly more complex. * Slower execution: Since two method calls are made, this option may be slower in terms of execution time compared to Option 1. **Library Usage** The benchmark uses the HTML5 Canvas API, which is a built-in feature of modern browsers. The `document.getElementById()` and `getContext()` methods are used to interact with the canvas element. **Special JavaScript Feature or Syntax** None mentioned in this benchmark. **Other Alternatives** If you were to rewrite this benchmark using other approaches: * **Using a library**: You could use a library like Canvas.js, which provides more advanced features for working with canvases. However, this would add dependencies and complexity to the benchmark. * **Using hardware acceleration**: You could also test the performance of each option when using hardware acceleration (e.g., WebGL). This would require more complex setup and might not be necessary for a simple canvas benchmark. * **Using a different drawing approach**: Depending on your specific use case, you might consider using alternative drawing approaches like SVG or pixel manipulation. However, these alternatives would likely introduce new complexities and trade-offs. Overall, the choice of option depends on the specific requirements of your project and the trade-offs you're willing to make between performance, accuracy, and simplicity.
Related benchmarks:
save/restore vs manual
Canvas clearing performance v3
Overhead of saving and restoring context state 2.0
Overhead of saving and restoring context state 2
Comments
Confirm delete:
Do you really want to delete benchmark?