Skip to content

Instantly share code, notes, and snippets.

@justinbmeyer
Created June 12, 2012 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinbmeyer/2913777 to your computer and use it in GitHub Desktop.
Save justinbmeyer/2913777 to your computer and use it in GitHub Desktop.
Fast Workers with strings

I should be able to send the same message to many workers with the same message with no incurred copying cost. This is because there is no way to modify a string in JavaScript, only create new strings. So, I should be able to call:

javascript var bigString = " .... "; worker1.postMessage(bigString); worker2.postMessage(bigString); ...


And it should be smart enough to pass the reference around and not copy anything.  The only copying would be done later if I read or created substr's of bigString.

## Use case 

Say I wanted to unzip a folder of files.  I'd unzip first to get the folder structure, then I'd need to unzip each file.  I could create a worker for each file, and pass all the data to each worker.  Then I'd send each worker another message telling it where it's file is.   It would read that section and unzip it. 

This way any copying would be done in the worker threads.  Even better would be having the initial unzipping done in a worker thread, that message gets passed to the page, which would immediately spawn more workers and relay the message.  But, the message would not be copied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment