设备内存 API
备注: 此特性在 Web Worker 中可用。
客户端设备的性能在很大程度上取决于可用内存(RAM)的大小。传统上,开发人员必须使用启发式方法,要么对设备进行基准测试,要么根据设备制造商或用户代理字符串等其他参数推断设备的性能。
确定设备内存
有两种方法可以确定设备的 RAM 大小:使用设备内存 JavaScript API 或接受客户端提示。
JavaScript API
你可以通过检索 Navigator.deviceMemory
或 WorkerNavigator.deviceMemory
来查询设备的近似 RAM 的大小。
js
const RAM = navigator.deviceMemory;
客户端提示
你也可以使用客户端提示 HTTP 标头与 Device-Memory
指令来检索设备的近似 RAM 大小。
接口
对其他接口的扩展
-
返回以 GB 为单位的设备内存的近似大小。
-
返回以 GB 为单位的设备内存的近似大小。
规范
Specification |
---|
Device Memory |
浏览器兼容性
api.Navigator.deviceMemory
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
deviceMemory |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
api.WorkerNavigator.deviceMemory
Report problems with this compatibility data on GitHubdesktop | mobile | server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
deviceMemory |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
http.headers.Device-Memory
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Device-Memory request header |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.