1

this is a array object and how i call itI need to find a way to send an array containing methods to a web worker , but I do not need the methods to do what I want to do , however I do still need to loop through the array in the worker.

postmessage(jsEngine.gameObjects)- gives error for structured cloning; (JsEngine.gameObjects is a object filled with other objects as its properties)

onRadar() {
  let otherKey = Object.keys(jsEngine.gameObjects);
  for (let i = 0; i < otherKey.length; i++) {

    // let otherObjectnames = otherKey[i];
    let otherObject = jsEngine.gameObjects[otherKey[i]];
    if (this.name === otherObject.name)
      continue;
    if (otherObject === undefined)
       continue;
    // Check if the other object is in the vicinity of this one
    let distance = calc_distance(this.transform, otherObject.transform);
    if (distance <= this.sensorRange && distance <= this.sensorRange) {
    }
  }
}
static660
  • 81
  • 1
  • 13
  • 3
    why do you post a screenshot of your code and not the code :( – Isaac Sep 21 '17 at 00:46
  • What is `onRadar`? Is that the webworker code? How does it relate to the `postmessage` call? Can you provide enough to reproduce the issue? – trincot Sep 21 '17 at 15:45
  • I want to replace this code with the web worker to run every second , its to take the load off the main thread . – static660 Sep 22 '17 at 05:23
  • You could send a `JSON.stringify` representation, and even [clean its cyclics objects beforehand](https://stackoverflow.com/questions/46190722/localstorage-save-and-load-js-object/46191107#46191107), or even the `JSON.parse` representation of this string, but if you are willing to call it every seconds, I'm not sure it will *take off the load of the main thread* at all, since you will eat a lot of memory which would have to be GCed every now and then. – Kaiido Sep 22 '17 at 06:09
  • can you give an example because ive tried using stringify and using the same approach i would use just with the object on the main , on the seperate thread it shows just [] as the array and does not show properties – static660 Sep 22 '17 at 06:52
  • @Kaiido I figured it out and i have found a way to make the load a lot less however , you mentioned that i need to do garbage collection . on what might that refer to and how would i go about it do you think ? . the objective is to send just the x and y coordinates of my objects along with an id to the web worker and it will search through them to see if they are in distance . – static660 Sep 29 '17 at 01:10
  • 1/2) What I meant is that if you do swallow copies of your objects, then the browser would have to GC it (it will keep in memory the one version on the main thread, will create a String version of it to clean it from uncloneable objects, [if your parse this string again, then it will store a new Object in memory], then will create a copy of what you send to the worker (string or object) that will be consumed by the worker and marked for GC after it has done its job, then create an other copy of the results to be send back to main. – Kaiido Sep 29 '17 at 01:23
  • 2/2) At the end only the result and original object will stay in memory, all other copies will be marked for GC, which means that it will kick really often. GC is one of the slowest operations a browser can do, on which you have no direct control. So yes, if all you need is `{x,y,id}` and that you are able to keep all the needed data always active in the worker, then you'd be better just mapping your array to this minimal version instead of cloning a lot of times bigger objects. – Kaiido Sep 29 '17 at 01:23
  • @kaiido I have realized allot lately that a big majority of Web Dev's have no idea what their doing. judging by your answer and your profile/history/Q/A, you know what your doing. I hope if I could contact you on social media to ask you questions if you have time?. – static660 Oct 01 '17 at 06:52

0 Answers0