Skip to content

Instantly share code, notes, and snippets.

@wookiehangover
Created September 3, 2013 04:36
Show Gist options
  • Save wookiehangover/6419759 to your computer and use it in GitHub Desktop.
Save wookiehangover/6419759 to your computer and use it in GitHub Desktop.
Turn a function into an inline web worker
(function(){
var Blob = this.Blob;
var URL = this.URL;
var Worker = this.Worker;
this.createWorker = function(src){
src = src.toString();
var srcLines = src.split('\n');
// Remove the outer function. There's probably a better way.
if( /^f.+\{/.test(srcLines[0]) ){
srcLines.shift();
}
if( /\}/.test(srcLines[srcLines.length - 1]) ){
srcLines.pop();
}
var blob = new Blob([ srcLines.join('\n') ], { type: "application/javascript" });
var blobUrl = URL.createObjectURL(blob);
var worker = new Worker( blobUrl );
return worker;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment