feat: Optimize object iteration and module counting

This commit is contained in:
kmmiio99o 2026-03-06 18:32:05 +01:00
parent ad93bfa6c6
commit 612483fbea
No known key found for this signature in database
GPG key ID: 163FE39F7772D2EC
2 changed files with 13 additions and 13 deletions

View file

@ -27,10 +27,13 @@ function treeSearch(tree: SearchTree, filter: SearchFilter, opts: Required<FindI
} catch { }
}
} else if (typeof tree === "object") {
for (const key of Object.keys(tree)) {
const walkableLen = opts.walkable.length;
const ignoreLen = opts.ignore.length;
for (const key in tree) {
if (Object.prototype.hasOwnProperty.call(tree, key) === false) continue;
if (typeof tree[key] !== "object" || tree[key] === null) continue;
if (opts.walkable.length && !opts.walkable.includes(key)) continue;
if (opts.ignore.includes(key)) continue;
if (walkableLen && !opts.walkable.includes(key)) continue;
if (ignoreLen && opts.ignore.includes(key)) continue;
try {
const found = treeSearch(tree[key], filter, opts, depth + 1);

View file

@ -16,23 +16,18 @@ let _metroCache = null as unknown as ReturnType<typeof buildInitCache>;
export const getMetroCache = () => _metroCache;
function buildInitCache() {
let modulesCount = 0;
for (const _ in window.modules) modulesCount++;
const cache = {
_v: CACHE_VERSION,
_buildNumber: NativeClientInfoModule.getConstants().Build,
_modulesCount: Object.keys(window.modules).length,
_modulesCount: modulesCount,
flagsIndex: {} as Record<string, number>,
findIndex: {} as Record<string, ModulesMap | undefined>,
polyfillIndex: {} as Record<string, ModulesMap | undefined>
} as const;
// Force load all modules so useful modules are pre-cached. Add a minor
// delay so the cache is initialized before the modules are loaded.
setTimeout(() => {
for (const id in window.modules) {
require("./modules").requireModule(id);
}
}, 100);
_metroCache = cache;
return cache;
}
@ -52,7 +47,9 @@ export async function initMetroCache() {
_metroCache = null!;
throw "cache invalidated; version mismatch";
}
if (_metroCache._modulesCount !== Object.keys(window.modules).length) {
let currentCount = 0;
for (const _ in window.modules) currentCount++;
if (_metroCache._modulesCount !== currentCount) {
_metroCache = null!;
throw "cache invalidated; modules count mismatch";
}