1.用户设备的RAM if (navigator.deviceMemory > 1) { await import('./costly-module.js'); } //浏览器支持仅限于基于Chromium的浏览器,根据caniuse的说法,这是全球使用情况统计的70%。如果navigator.deviceMemory为undefined,则可以考虑是否加载JavaScript。 2.用户设备有足够的cpu if (navigator.hardwareConcurrency > 4) { await import('./costly-module.js'); } // 逻辑处理器内核的数量可用于测量可以立即有效运行的线程数量,而无需上下文切换。 3.剩余电量 // { level: 0.53 (53%), charging: true...} const {level, charging} = await navigator.getBattery(); // If the device is currently charging // Or the battery level is more than 20% if (charging || level > 0.2) { await import('./costly-module.js'); } 4.设备有足够的存储空间时加载JavaScript const {quota} = await navigator.storage.estimate(); const fiftyMegabytesInBytes = 50 * 1e+6; if (quota > fiftyMegabytesInBytes) { await import('./costly-module.js'); } 5.设备具有良好的网络连接时加载JavaScript // ⚠️ 4g does not mean fast! if (navigator.connection.effectiveType === '4g') { await import('./costly-module.js'); }