Get number of CPU cores in JavaScript?

前端 未结 5 1549
执笔经年
执笔经年 2020-12-05 09:00

Is there a way to determine the number of available CPU cores in JavaScript, so that you could adjust the number of web workers depending on that?

5条回答
  •  無奈伤痛
    2020-12-05 09:53

    If you are going to do data crunching on your workers, navigator.hardwareConcurrency might not be good enough as it returns the number of logical cores in modern browsers, you could try to estimate the number of physical cores using WebCPU:

    import {WebCPU} from 'webcpu';
    
    WebCPU.detectCPU().then(result => {
        console.log(`Reported Cores: ${result.reportedCores}`);
        console.log(`Estimated Idle Cores: ${result.estimatedIdleCores}`);
        console.log(`Estimated Physical Cores: ${result.estimatedPhysicalCores}`);
    });
    

提交回复
热议问题