Skip to content

Commit 0249d4e

Browse files
authored
cache the result of isAppleSilicon() (#1828)
* cache the result of isAppleSilicon() * Lint fix * Fix the value calculation
1 parent 87c0feb commit 0249d4e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

injected/src/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ let globalObj = typeof window === 'undefined' ? globalThis : window;
77
let Error = globalObj.Error;
88
let messageSecret;
99

10+
let isAppleSiliconCache = null;
11+
1012
// save a reference to original CustomEvent amd dispatchEvent so they can't be overriden to forge messages
1113
export const OriginalCustomEvent = typeof CustomEvent === 'undefined' ? null : CustomEvent;
1214
export const originalWindowDispatchEvent = typeof window === 'undefined' ? null : window.dispatchEvent.bind(window);
@@ -258,13 +260,18 @@ export function camelcase(dashCaseText) {
258260

259261
// We use this method to detect M1 macs and set appropriate API values to prevent sites from detecting fingerprinting protections
260262
function isAppleSilicon() {
263+
// Cache the result since hardware doesn't change
264+
if (isAppleSiliconCache !== null) {
265+
return isAppleSiliconCache;
266+
}
261267
const canvas = document.createElement('canvas');
262268
const gl = canvas.getContext('webgl');
263269

264270
// Best guess if the device is an Apple Silicon
265271
// https://stackoverflow.com/a/65412357
266-
// @ts-expect-error - Object is possibly 'null'
267-
return gl.getSupportedExtensions().indexOf('WEBGL_compressed_texture_etc') !== -1;
272+
const compressedTextureValue = gl?.getSupportedExtensions()?.indexOf('WEBGL_compressed_texture_etc');
273+
isAppleSiliconCache = typeof compressedTextureValue === 'number' && compressedTextureValue !== -1;
274+
return isAppleSiliconCache;
268275
}
269276

270277
/**

0 commit comments

Comments
 (0)