Does a Firefox Workers limit exist?

后端 未结 4 1889
遇见更好的自我
遇见更好的自我 2020-12-18 04:47

Im trying to create web Workers and post messages to them in cycle:

array.forEach(function (data) {
        this.createWorker();
        this.workers[this.wo         


        
相关标签:
4条回答
  • 2020-12-18 05:26

    There is a setting in Firefox, called "dom.workers.maxPerDomain" which is by default 20.

    However, there might not be any real performance gain in using more workers than you have cores in the computer. With a modern computer today that has hyper threading, I think using around 8 workers would be sufficient. Otherwise you might cause to much context switching that would instead introduce a bottleneck.

    It all depends though, what you want to achieve.

    0 讨论(0)
  • 2020-12-18 05:37

    I also played around with workers and tried to find an optimum for my case (encryption of strings). It was 8 too.

    Similar question and discussion: Number of Web Workers Limit

    0 讨论(0)
  • 2020-12-18 05:39

    Just did some test of my own. For this, i changed the code a little bit:

    Cycle:

    for(var i=0;i<200;i++){
       this.createWorker();
       this.workers[this.workersPointer].postMessage({task: 'someTask', number:i});
    };
    

    createWorker function:

    this.workers =[];
    this.workersPointer = 0;
    storage=[];
    
    
    var createWorker= function () {
        workersPointer++;
        var myPointer = workersPointer;
        var worker = this.workers[this.workersPointer] = new Worker('Worker.js');
    
        worker.onmessage = function (event) {
            if (event.data.error) {
                alert(event.data.error);
            }
            else {
                document.cookie=event.data.task+"["+myPointer+"]="+event.data.number;
            }
        };
        worker.onerror = function (event) {
            alert("Error: " + event.error);
        };
    }
    

    Worker:

    onmessage = function(event) {
        postMessage({number:event.data.number*2, task: event.data.task});
    };
    

    After i run this, in chrome i got 66 cookies (including a nice blue crash window), in firefox i got 20. So both browsers seem to have worker limitations.

    EDIT:

    In Opera i get a console message:

    Maximum number of Web Worker instances(16) exceeded for this window.

    0 讨论(0)
  • 2020-12-18 05:43

    For futher reference check out in Firefox

    about:config

    There's a parameter called :

    dom.workers.maxPerDomain

    Wich (at least in FF 33) is set to a default value of 20.

    gl.

    And as noted on this other stackoverflow question:

    Each browser has web workers limitations (Firefox has 20, Chrome 60+, Opera 16); however, you can change it in Firefox -> dom.workers.maxPerDomain; as for your actual question, if you can or cannot avoid this limitation, I'm not sure. "Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers." Can you give an exact situation where you would want to use more than 20 workers? – Marius Balaban Nov 26 '12 at 22:34

    0 讨论(0)
提交回复
热议问题