The Official Power User Tips, Tricks & Productivity Thread

bnew

Veteran
Joined
Nov 1, 2015
Messages
57,343
Reputation
8,496
Daps
160,027
Trying out this userscript on YouTube atm because that site has been running like :trash: lately


(They have put some of their userscripts on GreasyFork but I went to their full collection on GitHub)

will test it out,:ehh:




### Summary of the UserScript: YouTube Fix for Memory Leakage

This user script is designed to address a memory leakage issue on YouTube, particularly when using certain browsers. Here’s a detailed explanation:

#### Purpose
The script aims to prevent memory leaks that can occur when YouTube loads and manages various elements on its pages. Memory leaks happen when the browser fails to release memory allocated for elements that are no longer needed, leading to performance issues and increased memory usage over time.

#### How It Works

1. **Promise Constructor Workaround**:
- The script starts by defining a `Promise` constructor because YouTube sometimes hacks the `Promise` object in certain browsers (like WaterFox Classic), which prevents promises from resolving properly.
- This ensures that promises work correctly even in those browsers.

2. **Element Inspection Functions**:
- Two helper functions, `insp` and `indr`, are defined to inspect and retrieve specific properties of DOM elements.
- These functions help in identifying and manipulating the elements that might be causing memory leaks.

3. **Thumbnail Selection**:
- A function called `getThumbnail` is used to select the highest resolution thumbnail from a list of thumbnails.
- This is useful for ensuring that only necessary thumbnails are loaded, reducing unnecessary data transfer and potential memory usage.

4. **WeakMap for Element Tracking**:
- A `WeakMap` named `ytDOMWM` is created to track whether certain DOM elements should use patched lifecycles.
- A `WeakMap` is used here because it allows garbage collection of elements once they are no longer referenced, which helps in preventing memory leaks.

5. **Property Definition on Element Prototype**:
- The script defines a property called `usePatchedLifecycles` on the `Element.prototype`.
- This property has getter and setter methods.
- The getter checks if an element should use patched lifecycles based on its type and connection status.
- The setter sets whether an element should use patched lifecycles and updates the `WeakMap` accordingly.

6. **Element Type Checking**:
- The script checks the type of each element (e.g., `yt-attributed-string`, `yt-image`, etc.) and decides whether it should apply patched lifecycles based on predefined rules.
- For example, certain elements like `yt-player-seek-continuation` or `yt-payments-manager` will always have patched lifecycles applied.
- For others like `yt-img-shadow`, it checks if there is a valid thumbnail URL before applying patched lifecycles.

7. **Thumbnail Update for Images**:
- If an image element (`yt-img-shadow`) has a valid thumbnail URL, the script updates the image source using this URL after a short delay using `Promise.resolve(0).then()`.
- This ensures that only necessary images are loaded and displayed.

### Impact

- **Memory Efficiency**: By ensuring that only necessary elements are kept in memory and by properly managing their lifecycles, this script helps reduce memory leaks.
- **Performance Improvement**: Reducing memory leaks can improve overall browser performance when using YouTube, especially during extended sessions or with multiple tabs open.
- **Compatibility**: The script is designed to work across various browsers including Chrome, Firefox, Opera, Edge, and Safari.

In summary, this user script fine-tunes how YouTube manages its DOM elements to prevent unnecessary memory usage and improve overall performance by ensuring that elements are properly cleaned up when they are no longer needed.
 
Top