File tree Expand file tree Collapse file tree 1 file changed +16
-3
lines changed
packages/browser-utils/src Expand file tree Collapse file tree 1 file changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,14 @@ interface CacheableImplementations {
15
15
16
16
const cachedImplementations : Partial < CacheableImplementations > = { } ;
17
17
18
+ /**
19
+ * isNative checks if the given function is a native implementation
20
+ */
21
+ // eslint-disable-next-line @typescript-eslint/ban-types
22
+ function isNative ( func : Function ) : boolean {
23
+ return func && / ^ f u n c t i o n \s + \w + \( \) \s + \{ \s + \[ n a t i v e c o d e \] \s + \} $ / . test ( func . toString ( ) ) ;
24
+ }
25
+
18
26
/**
19
27
* Get the native implementation of a browser function.
20
28
*
@@ -32,8 +40,14 @@ export function getNativeImplementation<T extends keyof CacheableImplementations
32
40
return cached ;
33
41
}
34
42
35
- const document = WINDOW . document ;
36
43
let impl = WINDOW [ name ] as CacheableImplementations [ T ] ;
44
+
45
+ // Fast path to avoid DOM I/O
46
+ if ( isNative ( impl ) ) {
47
+ return ( cachedImplementations [ name ] = impl . bind ( WINDOW ) as CacheableImplementations [ T ] ) ;
48
+ }
49
+
50
+ const document = WINDOW . document ;
37
51
// eslint-disable-next-line deprecation/deprecation
38
52
if ( document && typeof document . createElement === 'function' ) {
39
53
try {
@@ -62,8 +76,7 @@ export function getNativeImplementation<T extends keyof CacheableImplementations
62
76
63
77
/** Clear a cached implementation. */
64
78
export function clearCachedImplementation ( name : keyof CacheableImplementations ) : void {
65
- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
66
- delete cachedImplementations [ name ] ;
79
+ cachedImplementations [ name ] = undefined ;
67
80
}
68
81
69
82
/**
You can’t perform that action at this time.
0 commit comments