Skip to content

Commit 40ad66d

Browse files
committed
caching classes speeds it up by around 10%
1 parent ea16a3f commit 40ad66d

File tree

1 file changed

+13
-2
lines changed
  • lib/src/nativescript-ngx-shadow/common

1 file changed

+13
-2
lines changed

lib/src/nativescript-ngx-shadow/common/shadow.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,20 @@ if (isAndroid) {
2121
PlainShadow = android.graphics.drawable.GradientDrawable.extend({});
2222
}
2323

24+
const classCache: { [id: string]: { class: any, fieldCache: { [id: string]: any } } } = {};
2425
// https://github.com/NativeScript/android-runtime/issues/1330
2526
function getAndroidR(rtype: string, field: string): number {
26-
return +java.lang.Class.forName("android.R$" + rtype).getField(field).get(null);
27+
const className = "android.R$" + rtype;
28+
if (!classCache.hasOwnProperty(className)) {
29+
classCache[className] = {
30+
class: java.lang.Class.forName(className),
31+
fieldCache: {}
32+
};
33+
}
34+
if(!classCache[className].fieldCache.hasOwnProperty(field)) {
35+
classCache[className].fieldCache[field] = classCache[className].class.getField(field);
36+
}
37+
return +classCache[className].fieldCache[field].get(null);
2738
}
2839

2940
export class Shadow {
@@ -202,7 +213,7 @@ export class Shadow {
202213
nativeView.layer.shouldRasterize = data.rasterize;
203214
nativeView.layer.rasterizationScale = screen.mainScreen.scale;
204215
let shadowPath = null;
205-
if(data.useShadowPath) {
216+
if (data.useShadowPath) {
206217
shadowPath = UIBezierPath.bezierPathWithRoundedRectCornerRadius(nativeView.bounds, nativeView.layer.shadowRadius).cgPath;
207218
}
208219
nativeView.layer.shadowPath = shadowPath;

0 commit comments

Comments
 (0)