Search code examples
javascriptperformancecpuuser-experience

Is there a way to know how "busy" someone's machine is from a browser's JS?


If my page's JS has an obnoxiously long and computationally heavy operation, is there a quick and dirty way to know if they are AFK with no apps on or if they're mining bitcoin in 1000 threads?

In other words, is there a way to roughly gauge how "busy" a CPU is via JavaScript?

I'm referencing code that is being run by an extension.

I was thinking about running a hashing loop for a a few hundred milliseconds and get a report on how long it took, however this doesn't take into account background processes, overall CPU power, et cetera.


Solution

  • I'm referencing code that is being run by an extension.

    If you by extension mean WebExtension there is the Idle API* you can use to queryState(). However, the IdleState result can only return the states "active", "idle" or "locked".

    The latter two should be useful though.

    *) The API is not fully supported in Edge at this moment.