-
Notifications
You must be signed in to change notification settings - Fork 319
Added RAM usage extension #2379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Name: RAM Monitor | ||
| // ID: bitterRamUsage | ||
| // Description: Monitors current RAM usage and percentage. | ||
| // By: DevidBittFive <https://scratch.mit.edu/users/Bitter_160/> | ||
| // License: EPL-2.0 | ||
|
|
||
| /**! | ||
| * @license EPL-2.0 | ||
| * All code contained in this extension is licensed exclusively under the Eclipse Public License v2.0. | ||
| * | ||
| * If you have not received a copy of this license, you may access it | ||
| * at the following link: https://www.eclipse.org/legal/epl-2.0/ | ||
| */ | ||
|
|
||
| (function (Scratch) { | ||
| "use strict"; | ||
|
|
||
| // Cache once outside the class | ||
| const mem = | ||
| globalThis.performance && globalThis.performance.memory | ||
| ? globalThis.performance.memory | ||
| : null; | ||
|
|
||
| class RamUsageExtension { | ||
| getInfo() { | ||
| return { | ||
| id: "bitterRamUsage", | ||
| name: Scratch.translate("RAM Monitor"), | ||
| blocks: [ | ||
| { | ||
| blockType: Scratch.BlockType.XML, | ||
| xml: mem | ||
| ? "" | ||
| : ` | ||
| <sep gap="-12" /> | ||
| <label text="${Scratch.translate("Your browser does not")}" /><sep gap="-12" /> | ||
| <label text="${Scratch.translate("support the "performance.memory"")}" /><sep gap="-12" /> | ||
| <label text="${Scratch.translate("API")}" /> | ||
| `, | ||
| }, | ||
| { | ||
| opcode: "getRamUsageMB", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("get RAM usage in MB"), | ||
| }, | ||
| { | ||
| opcode: "getRamUsagePercent", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("get RAM usage percent"), | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| getRamUsageMB() { | ||
| // Check the cached mem variable directly | ||
| if (!mem) return NaN; | ||
|
|
||
| return mem.usedJSHeapSize / 1024 / 1024; | ||
| } | ||
|
|
||
| getRamUsagePercent() { | ||
| // Simplified check | ||
| if (!mem) return NaN; | ||
|
|
||
| // Calculate percentage as a raw number | ||
| return (mem.usedJSHeapSize / mem.jsHeapSizeLimit) * 100; | ||
| } | ||
| } | ||
|
|
||
| Scratch.extensions.register(new RamUsageExtension()); | ||
| })(Scratch); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.