Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Overhead of saving and restoring context state 3
(version: 0)
Comparing performance of:
Not saving and restoring context state vs Saving and restoring context state
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:
Not saving and restoring context state
for (let i=0; i<$iterations; ++i) { $context.fillStyle = 'rgba(100,150,200,0.9)'; $context.fillRect(0, 0, 1, 1); }
Saving and restoring context state
for (let i=0; i<$iterations; ++i) { const {fillStyle} = $context; $context.fillStyle = 'rgba(100,150,200,0.9)'; $context.fillRect(0, 0, 1, 1); $context.fillStyle = fillStyle; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Not saving and restoring context state
Saving and restoring context state
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.2294 YaBrowser/23.9.0.2294 Yowser/2.5 Safari/537.36
Browser/OS:
Yandex Browser 23 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Not saving and restoring context state
1447.6 Ops/sec
Saving and restoring context state
792.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the provided benchmark. **Benchmark Overview** The benchmark measures the overhead of saving and restoring context state in JavaScript. It tests two approaches: one where the context state is not saved, and another where it is saved. **Script Preparation Code** The script preparation code sets up a canvas element and its 2D drawing context: ```javascript $canvas = document.getElementById('canvas'); $context = $canvas.getContext('2d'); $context.clearRect(0, 0, 800, 300); ``` This clears the canvas to ensure a consistent starting point for each benchmark run. **Html Preparation Code** The HTML preparation code defines an empty canvas element with a width and height: ```html <canvas id="canvas" width="800" height="300"></canvas> ``` This ensures that all browsers will see the same initial state for the canvas. **Individual Test Cases** There are two test cases: 1. **Not saving and restoring context state** ```javascript for (let i=0; i<$iterations; ++i) { $context.fillStyle = 'rgba(100,150,200,0.9)'; $context.fillRect(0, 0, 1, 1); } ``` This test case simply draws a single pixel on the canvas repeatedly using the `fillStyle` property directly. 2. **Saving and restoring context state** ```javascript for (let i=0; i<$iterations; ++i) { const {fillStyle} = $context; $context.fillStyle = 'rgba(100,150,200,0.9)'; $context.fillRect(0, 0, 1, 1); $context.fillStyle = fillStyle; } ``` This test case saves the `fillStyle` property before drawing a pixel, restores it immediately after drawing, and then draws another pixel. This simulates saving and restoring context state. **Pros and Cons** * **Not Saving and Restoring Context State** + Pros: Simple and straightforward, minimal overhead. + Cons: May lead to inconsistent results due to caching or other optimizations. * **Saving and Restoring Context State** + Pros: More accurate representation of real-world use cases, can help identify performance issues related to context state management. + Cons: Adds unnecessary overhead, may slow down benchmarked code. **Library** In this benchmark, `$context` is an alias for the 2D drawing context of a `<canvas>` element. The `fillStyle` property is used to set the color and alpha value for drawing pixels on the canvas. **Special JS Feature or Syntax** None are explicitly mentioned in the provided code. However, it's worth noting that the use of template literals (e.g., `$iterations = 500;`) is a feature introduced in ECMAScript 2015 (ES6). **Alternatives** Other ways to measure context state overhead might include: * Using a library like `fast-canvas` or `jsdom` for more controlled canvas rendering. * Adding additional rendering steps between the two test cases to increase the difference in execution time. * Using different caching mechanisms or optimization techniques in the benchmarked code. Keep in mind that these alternatives may change the nature of the benchmark and affect its results.
Related benchmarks:
Lodash.js Each vs Native Objects Keys and Each
map vs object - key access 3
Object key access vs array find vs Set
reassigning an object with 10000 objects
drawImage: Copy from image or canvas fixed
Comments
Confirm delete:
Do you really want to delete benchmark?