37

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

AlejandroJS
  • 1,015
  • 2
  • 11
  • 29

6 Answers6

19

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

Community
  • 1
  • 1
protoEvangelion
  • 4,355
  • 2
  • 29
  • 38
6

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...

andy
  • 8,775
  • 13
  • 77
  • 122
  • Even if your app is not targeting Electron, it's possible to deploy any SPA to Electron and you can monitor the CPU usage there. – M.K. Safi May 12 '20 at 23:03
1

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 would measure the CPU speed of looping.

halfer
  • 19,824
  • 17
  • 99
  • 186
0

On nodeJS there is a library : systeminformation npm systeminformation This library provides system information like CPU, Memory, battery, and more, on several OS: Windows, Linux, Sun, and Mac. It requires Node, I do not think it would work on webapp.

Ida Amit
  • 1,411
  • 2
  • 13
  • 27
0

Try using

navigator.hardwareConcurrency

Example:

alert(navigator.hardwareConcurrency);

<html>

<body>
  <h1>The CPU usage will be logged to the console.</h1>
  <script>
    console.log(navigator.hardwareConcurrency);
  </script>
</body>

</html>
Best Codes
  • 11
  • 3
0

Since the question says:

on the browser

Maybe it's helpful to know that Microsoft Edge(not sure if it's exclusive) has a tab called "Performance Monitor" inside it's dev tools. It shows CPU Usage, JS Heap size, etc.

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34570698) – MCGRAW Jun 22 '23 at 22:30
  • @MCGRAW I thought I was doing this by mentioning the tab's existence inside the dev tools. I'm not sure what else I would need to provide, the link is just a more extensive explanation of what the performance monitor does. The link changing wouldn't change the answer. Could you please clarify what I need to add or change to give a complete answer? – Vasil Verdouw Jun 26 '23 at 23:28