js: improve performance of instanceof against interfaces

This commit is contained in:
Alexey Andreev 2024-04-26 10:37:12 +02:00
parent 19518eb191
commit 188d189407

View File

@ -21,15 +21,28 @@ let $rt_isAssignable = (from, to) => {
if (from === to) { if (from === to) {
return true; return true;
} }
let map = from.$meta.assignableCache;
if (typeof map === 'undefined') {
map = new Map();
from.$meta.assignableCache = map;
}
let cachedResult = map.get(to);
if (typeof cachedResult !== 'undefined') {
return cachedResult;
}
if (to.$meta.item !== null) { if (to.$meta.item !== null) {
return from.$meta.item !== null && $rt_isAssignable(from.$meta.item, to.$meta.item); let result = from.$meta.item !== null && $rt_isAssignable(from.$meta.item, to.$meta.item);
map.set(to, result);
return result;
} }
let supertypes = from.$meta.supertypes; let supertypes = from.$meta.supertypes;
for (let i = 0; i < supertypes.length; i = (i + 1) | 0) { for (let i = 0; i < supertypes.length; i = (i + 1) | 0) {
if ($rt_isAssignable(supertypes[i], to)) { if ($rt_isAssignable(supertypes[i], to)) {
map.set(to, true);
return true; return true;
} }
} }
map.set(to, false);
return false; return false;
} }
let $rt_castToInterface = (obj, cls) => { let $rt_castToInterface = (obj, cls) => {