From 188d189407fe5b8e9915f907457590868bcac693 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 26 Apr 2024 10:37:12 +0200 Subject: [PATCH] js: improve performance of `instanceof` against interfaces --- .../org/teavm/backend/javascript/reflection.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/src/main/resources/org/teavm/backend/javascript/reflection.js b/core/src/main/resources/org/teavm/backend/javascript/reflection.js index c8cb67944..fb454eb38 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/reflection.js +++ b/core/src/main/resources/org/teavm/backend/javascript/reflection.js @@ -21,15 +21,28 @@ let $rt_isAssignable = (from, to) => { if (from === to) { 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) { - 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; for (let i = 0; i < supertypes.length; i = (i + 1) | 0) { if ($rt_isAssignable(supertypes[i], to)) { + map.set(to, true); return true; } } + map.set(to, false); return false; } let $rt_castToInterface = (obj, cls) => {