0

I am using web workers to do intensive computer vision stuff. How it works is in the web worker, I call post message to send data to the parent, but it gets called a lot of times in high frequency (thousands). Currently the program runs slow too. I used chrome dev tools to profile the js usage, and I got this:

enter image description here

Does anyone know what this means. I think the post message is maybe taking the longest. Would it be faster if I collect all the data and then send back the full batch at once with 1 postmessage?

omega
  • 40,311
  • 81
  • 251
  • 474
  • What do you mean by "too slow"? – guest271314 Feb 15 '17 at 05:15
  • Like it takes a long time for the program to complete. – omega Feb 15 '17 at 05:15
  • What occurs at main thread? What is "a long time"? What is the time that you expect process to complete within? – guest271314 Feb 15 '17 at 05:16
  • maybe try to send `JSON. stringify()` to `postMessage` it does work faster when passing objects. – vardius Feb 15 '17 at 05:18
  • The parent (main), just calls a task on the web worker. The web worker figure out a value for each cell in the canvas 2d grid (imagedata), for each x,y, it calls the parent with the value, the parent sets the value (color) in the canvas grid. When the web worker finishes, the parent sets the 2d grid back into the canvas. – omega Feb 15 '17 at 05:18
  • It's not that it's running in an abnormally long time, its just that in whatever time it runs, I just want it to get it faster. – omega Feb 15 '17 at 05:19
  • Would it be faster, if I compute all the values in the web worker, then the web worker sends all the values back in 1 post message? – omega Feb 15 '17 at 05:20
  • Define "faster". You can post a single `ArrayBuffer` to main thread when entire process completes, see [createImageBitmap alternative on Safari](http://stackoverflow.com/questions/40094245/createimagebitmap-alternative-on-safari/) – guest271314 Feb 15 '17 at 05:21
  • I think, it would, also serialization takes more time then sending json object. Check this [benchmarks](https://nolanlawson.com/2016/02/29/high-performance-web-worker-messages/) – vardius Feb 15 '17 at 05:21
  • Doing that did make it a lot faster. – omega Feb 15 '17 at 06:10

0 Answers0