简体   繁体   中英

How to get CPU usage in JavaScript?

Is there a way to get the CPU(s) usage in JavaScript on the browser?

From what I have gathered, the most you can find out natively in the browser about JS CPU stats, is the amount of CPU cores the client is using. Insert this in your JS file:

console.log(navigator.hardwareConcurrency)

You can then check that in the Chrome Dev Tools console.

However, you can calculate the CPU load using Node.js . Here is a step-by-step on that.

The answer on this page may also be of help in your dilemma: Javascript- Dynamically monitor CPU/memory usage

Essentially, no. BUT... and this is a big BUT, if your application is an SPA you could consider deploying it via Electron.

If you did this you could then access the CPU usage: https://electronjs.org/docs/api/structures/cpu-usage

For you it's not a big leap to move from the web to Electron. But for your users it's a big change from accessing something in the browser to downloading an app. However, if this is a very important feature of your app/service then it might be worth it...

The answer you may be looking for is measuring the current CPU intensitivity.

 if((new Date()).getDay()==6){}else{ work = new Worker("data:text/javascript,setInterval(` dl=Date.now();for(itr=1;itr<1000;itr++){};dl=Date.now()-dl;postMessage(dl);`,1000);"); work.onmessage = (evt)=>{ console.info(12 - evt.data+(' point'+((new Intl.PluralRules(navigator.language)).select(12-evt.data)=='one'?'':'s'))) }; }

workers (not on Sabbath .) would measure the CPU speed of looping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM