{"id":11498,"date":"2024-12-23T00:31:51","date_gmt":"2024-12-22T16:31:51","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11498"},"modified":"2025-01-10T23:13:30","modified_gmt":"2025-01-10T15:13:30","slug":"js-get-memory-cpu-gpu-battery","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/12\/js-get-memory-cpu-gpu-battery\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528JS\u83b7\u53d6\u7cfb\u7edf\u5185\u5b58\u3001CPU\/GPU\u3001\u7535\u91cf\u7b49\u4fe1\u606f"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11498\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11498<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u72ec\u7acb\u57df\u540d\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202412-js-system.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/js-system-cover.jpg\" width=\"414\" height=\"259\" alt=\"\u5c01\u9762\u56fe JS\u7cfb\u7edf \u5723\u8bde\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p>\u76ee\u524d\uff0c\u6d4f\u89c8\u5668\u662f\u6709\u80fd\u529b\u83b7\u53d6\u7cfb\u7edf\u7684\u4e00\u4e9b\u4fe1\u606f\u7684\uff0c\u5305\u62ec\u5185\u5b58\u5927\u5c0f\uff0cCPU\/GPU\u3001\u7535\u91cf\u7b49\u3002<\/p>\n<h3>\u4e00\u3001JS\u83b7\u53d6\u5185\u5b58\u5927\u5c0f<\/h3>\n<p>\u5f88\u7b80\u5355\uff0c\u4e0b\u9762\u4e00\u884c\u4ee3\u7801\u5373\u53ef\uff1a<\/p>\n<pre>navigator.deviceMemory<\/pre>\n<p>\u4f8b\u5982\u5728\u6211\u7684Chrome\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u7684\u8f93\u51fa\u7ed3\u679c\u5c31\u662f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-20_201551.png\" width=\"269\" height=\"140\" alt=\"\u8bbe\u5907\u5185\u5b58\u5927\u5c0f\u83b7\u53d6\" class=\"alignnone size-medium\" \/><\/p>\n<p><code>deviceMemory<\/code>\u7684\u517c\u5bb9\u6027\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff0c\u76ee\u524d\u4ec5Chrome\u6d4f\u89c8\u5668\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-20_201721.png\" width=\"629\" height=\"216\" alt=\"deviceMemory\u8bbe\u5907\u5185\u5b58\u83b7\u53d6\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001JavaScript get cpu\u4fe1\u606f<\/h3>\n<p>\u76ee\u524dWeb\u6ca1\u6cd5\u83b7\u53d6CPU\u5177\u4f53\u7684\u53c2\u6570\u4fe1\u606f\uff0c\u4f46\u662f\u53ef\u4ee5\u83b7\u53d6CPU\u7684\u6838\u6570\u3002<\/p>\n<p>\u540c\u6837\u662fnavigator\u5bf9\u8c61\uff0c\u4e0d\u8fc7\u5c5e\u6027\u503c\u770b\u4e0d\u5230CPU\u4e09\u4e2a\u5b57\u6bcd\u7684\u5f71\u5b50\uff0c\u540d\u4e3ahardwareConcurrency\uff0c\u8868\u793a\u53ef\u4ee5\u5e76\u884c\u8fd0\u884c\u7684\u786c\u4ef6\u6570\u91cf\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>navigator.hardwareConcurrency<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-20_202201.png\" width=\"281\" height=\"110\" alt=\"CPU\u6838\u6570\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6ca1\u60f3\u5230\uff0c\u6211\u7684\u7834\u7535\u8111CPU\u662f12\u6838\u7684\uff0c\u5e94\u8be5\u4e0e\u53bb\u5e74\u6362\u4e3b\u677f\u6709\u5173\uff0c\u4e0d\u8fc7\u6ca1\u770b\u51fa\u6027\u80fd\u6709\u591a\u5c11\u63d0\u5347\u3002<\/p>\n<p>\u4e0d\u8fc7Mac\u4e0b\u8c8c\u4f3c\u53ef\u4ee5\u770b\u51fa\u82af\u7247\u7684\u54c1\u724c\uff0c\u4f8b\u5982\u6211\u7684Mac\u529e\u516c\u7535\u8111\u8fd0\u884c<code>navigator.platform<\/code>\u8fd4\u56de\u7684\u662f<code>'MacIntel'<\/code>\u3002<\/p>\n<p><code>hardwareConcurrency<\/code>\u503c\u7684\u517c\u5bb9\u6027\u6bd4<code>deviceMemory<\/code>\u597d\u591a\u4e86\uff0c\u53c2\u89c1\u4e0b\u56fe\uff0c\u53ef\u4ee5\u770b\u5230\u6240\u6709\u6807\u51c6\u6d4f\u89c8\u5668\u90fd\u662f\u652f\u6301\u7684\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-20_203158.png\" width=\"638\" height=\"321\" alt=\"\u786c\u4ef6\u5e76\u884c\u6570\u91cf\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001GPU\u4e5f\u662f\u53ef\u4ee5\u83b7\u53d6\u7684<\/h3>\n<p>\u4e5f\u662f\u4e00\u884c\u8bed\u6cd5\uff1a<\/p>\n<pre>navigator.gpu<\/pre>\n<p>\u4e0d\u8fc7<code>navigator.gpu<\/code>\u8fd4\u56de\u7684\u5e76\u4e0d\u662f\u5177\u4f53\u7684\u6570\u503c\uff0c\u800c\u662fGPU\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u548cWebGPU API\u5173\u8054\u5bc6\u5207\u3002<\/p>\n<p>\u652f\u6301\u4e00\u4e2a\u5c5e\u6027\u548c\u4e24\u4e2a\u65b9\u6cd5\uff0c\u5c5e\u6027\u662f<code>wgslLanguageFeatures<\/code>\uff0c\u65b9\u6cd5\u4e3a<code>requestAdapter()<\/code>\u548c<code>getPreferredCanvasFormat()<\/code>\u3002<\/p>\n<p>\u800c<code>navigator.gpu.wgslLanguageFeatures.size<\/code>\u8fd4\u56de\u7684\u662f\u5f53\u524dWeb\u73af\u5883\u652f\u6301\u7684WGSL\u8bed\u8a00\u7279\u5f81\u6570\u91cf\uff0c\u4f8b\u5982\u6211\u7684\u7535\u8111windows\u7535\u8111\u8fd4\u56de\u7684\u662f4\uff0cMac\u7b14\u8bb0\u672c\u4e5f\u662f4\uff0c\u6709\u4e9bGPU\u6bd4\u8f83\u8001\uff0c\u6570\u91cf\u5c31\u4e0d\u4f1a\u662f4.<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u4e2aAPI\u2026\u2026\u600e\u4e48\u8bf4\u5462\uff0c\u8ddf\u60f3\u8c61\u7684\u6709\u4e9b\u4e0d\u4e00\u6837\uff0c\u68c0\u6d4b\u7684\u5176\u5b9e\u662f\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301WebGPU\uff0c\u4ee5\u53ca\u662f\u5426\u53ef\u4ee5\u8c03\u7528\u9002\u914d\u5668\uff0c\u4ee5\u4f7f\u7528WebGL\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u6211\u770b\u6765\u4e0b\u517c\u5bb9\u6027\uff0c\u563f\uff0c\u6700\u65b0\u7684Safari\u548cFirefox\u90fd\u5df2\u7ecf\u652f\u6301\u4e86\uff0c\u8fd8\u633a\u5de7\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-20_204918.png\" width=\"629\" height=\"252\" alt=\"GPU\u5c5e\u6027\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u56db\u3001\u8bbe\u5907\u7684\u7535\u91cf\u8bc6\u522b<\/h3>\n<p>\u7535\u91cf\u83b7\u53d6\u4f7f\u7528getBattery()\u65b9\u6cd5\uff0c\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>let batteryIsCharging = false;\r\n\r\nnavigator.getBattery().then((battery) =&gt; {\r\n  \/\/ \u5f53\u524d\u7535\u91cf\u6c34\u5e73\u662f...\u8303\u56f40-1\r\n  console.log(battery.level);\r\n  \/\/ \u662f\u5426\u5728\u5145\u7535\r\n  batteryIsCharging = battery.charging;\r\n  \r\n  \/\/ \u5145\u7535\u72b6\u6001\u53d8\u5316\u7684\u65f6\u5019\r\n  battery.addEventListener(\"chargingchange\", () =&gt; {\r\n    batteryIsCharging = battery.charging;\r\n  });\r\n});<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u83b7\u6089\u5f53\u524d\u8bbe\u5907\u7684\u7535\u91cf\u72b6\u6001\uff0c\u4ee5\u53ca\u662f\u5426\u662f\u5145\u7535\u72b6\u6001\u3002<\/p>\n<p>\u50cf\u662fPC\u7535\u8111\uff0c\u63a5\u7535\u6e90\u7684\uff0c\u6ca1\u7535\u6c60\u7684\uff0c\u53ea\u8981\u8bbe\u5907\u8fd0\u884c\uff0c\u7535\u91cf\u8303\u56f4\u5c31\u4e00\u5b9a\u662f1\uff0c\u4f46\u662f\u79fb\u52a8\u7b14\u8bb0\u672c\u5c31\u4f1a\u4e0d\u4e00\u6837\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\uff0c\u663e\u793a\u81ea\u5b9a\u4e49\u7684\u7535\u6c60\u8fdb\u5ea6\uff0c\u5145\u7535\u52a8\u753b\uff0c\u6216\u8005\u4f4e\u7535\u91cf\u6a21\u5f0f\u7684\u4ea4\u4e92\u63d0\u9192\uff08\u4f8b\u5982\uff0c\u5173\u95ed\u4e0d\u5fc5\u8981\u7684\u52a8\u753b\u4ee5\u5e2e\u52a9\u7528\u6237\u7701\u7535\uff09\u7b49\u3002<\/p>\n<p>\u8fd9\u4e2aAPI\u5176\u5b9e\u548c\u7528\u6237\u4f53\u9a8c\u8fd8\u662f\u8d70\u5f97\u86ee\u8fd1\u7684\uff0c\u517c\u5bb9\u6027\u7684\u8bdd\uff0c\u6211\u770b\u770b\uff0c\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-20_205839.png\" width=\"628\" height=\"258\" alt=\"getBattery\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6709\u4e9b\u9057\u61be\uff0c\u4ec5\u4ec5Chrome\u6d4f\u89c8\u5668\u652f\u6301\uff0c\u602a\u4e0d\u5f97\u793e\u533a\u5f88\u5c11\u89c1\u5230\u6709\u5bf9\u6b64API\u7684\u8ba8\u8bba\u3002<\/p>\n<p>\u800c\u4e14\u4eceChrome\u5f00\u59cb\u652f\u6301\u7684\u65f6\u95f4\u6765\u770b\uff0c\u77ed\u65f6\u95f4\u5185\uff0c\u770b\u4e0d\u5230Safari\u6d4f\u89c8\u5668\u652f\u6301\u7684\u8ff9\u8c61\u3002<\/p>\n<p>\u8fd9\u4e48\u597d\u7684API\uff0c\u76ee\u524d\u4e5f\u53ea\u80fd\u5f53\u505a\u73a9\u5177\u4f7f\u7528\u4e86\u3002<\/p>\n<h3>\u4e94\u3001\u5176\u4ed6\u4e00\u4e9b\u786c\u4ef6\u4e0e\u8bbe\u5907\u7684\u8bc6\u522b<\/h3>\n<p><code>navigator<\/code>\u5bf9\u8c61\u8fd8\u63d0\u4f9b\u4e86\u5f88\u591a\u5176\u4ed6\u7684\u786c\u4ef6\u8bc6\u522b\uff0c\u4f8b\u5982\u84dd\u7259\uff0c\u865a\u62df\u952e\u76d8\uff0cUSB\uff0c\u5916\u7f6e\u6444\u50cf\u5934\uff0c\u662f\u5426\u6389\u7ebf\uff08navigator.onLine\uff09\u7b49\u3002<\/p>\n<p>\u7bc7\u5e45\u539f\u56e0\uff0c\u6211\u5c31\u4e0d\u4e00\u4e00\u5c55\u793a\u4e86\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u5e94\u7684\u6587\u6863\u3002<\/p>\n<p>\u53ea\u8981\u9047\u5230\u7c7b\u4f3c\u7684\u573a\u666f\uff0c\u77e5\u9053Web\u6709\u8fd9\u6837\u7684\u80fd\u529b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>ok\uff0c\u5c31\u8fd9\u6837\u5427\uff0c\u8f7b\u8f7b\u677e\u677e\uff0c\u53c8\u6c34\u4e86\u4e00\u7bc7\u6587\u7ae0\uff0c\u4f5b\u4f5b\u4f5b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/2024-12-23_002114.jpeg\" width=\"120\" height=\"120\" alt=\"\u4f5b\u4f5b\u4f5b\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11498\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11498<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202412\/js-system-cover.jpg\" width=\"160\" height=\"100\" alt=\"\u5c01\u9762\u56fe JS\u7cfb\u7edf \u5723\u8bde\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u6709\u4e9b\u524d\u7aef\u5f00\u53d1\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053\uff0cJavaScript\u662f\u6709\u80fd\u529b\u83b7\u53d6\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u3001CPU\/GPU\u3001\u7535\u91cf\u3001\u84dd\u7259\u3001\u6389\u7ebf\u7b49\u4fe1\u606f\u7684\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[215,2017,2020,2018,832,1289,2021,1254,2019],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11498"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11498"}],"version-history":[{"count":4,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11498\/revisions"}],"predecessor-version":[{"id":11502,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11498\/revisions\/11502"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}