web-worker
Advanced tools
Comparing version 0.0.0 to 0.1.0
{ | ||
"name": "web-worker", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "make web workers from strings", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "prova -l firefox -bq" | ||
}, | ||
@@ -18,3 +18,16 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/calvinmetcalf/web-worker" | ||
"homepage": "https://github.com/calvinmetcalf/web-worker", | ||
"dependencies": { | ||
"hbsify": "^1.0.1", | ||
"lie": "^2.7.7" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^5.12.0", | ||
"prova": "^1.14.2" | ||
}, | ||
"browserify": { | ||
"transform": [ | ||
"hbsify" | ||
] | ||
} | ||
} |
@@ -1,6 +0,59 @@ | ||
web-worker | ||
web-worker [](https://travis-ci.org/calvinmetcalf/web-worker) | ||
==== | ||
Simple web worker creation library, for use in browserify, pass it a string, gives you a blob worker. | ||
Harness the power of web workers from within JavaScript libraries. Allows you to either load a worker via a blob url or load the script in the same process via an iframe. As of Internet Explorer 12 all major browsers will support blob urls. This will allow you to package up JavaScript libraries that include worker components without needing to worry about cross origin issues. | ||
Expect things like shims to come soon. | ||
API | ||
=== | ||
if using browserify | ||
```js | ||
var webWorker = require('web-worker'); | ||
``` | ||
then give it a string or an array of strings | ||
```js | ||
var worker = webWorker([ | ||
'self.onmessage = function (e) {', | ||
' if (e.data === "ping") {', | ||
' self.postMessage("pong")', | ||
' }', | ||
'};' | ||
]); | ||
worker.onmessage = function (e) { | ||
console.log('message received', e.data); | ||
worker.terminate(); | ||
} | ||
worker.postMessage('ping'); | ||
``` | ||
The API of postMessege is | ||
```js | ||
worker.postMessage(object); | ||
worker.postMessage(object, array); | ||
worker.postMessage(object, port); | ||
``` | ||
It always takes a first parameter of an object, this is what is to be transfered to the worker (or back). | ||
Caveats | ||
=== | ||
- script imports need to be turned into absolute links to work from within blob workers and to load them in iframes. To do so we statically analyze them and put them at the top of the page, this means that even if they are inside of comments they will be loaded and programic use of `importScripts` is impossible from within the iframe and in the blob worker will cause it to not be made absolute. | ||
- not all errors inside of iframes will be caught | ||
- browsers that support workers but don't support blob workers will get an iframe shim instead of a real worker. | ||
Using Web Workers | ||
=== | ||
Web Workers can be powerful but not if used poorly, some tips: | ||
- Creating a worker is not free, it's actually a fairly expensive operation. So you should create a few workers that you can reuse as opposed to creating a large number of single use workers that you can create and destroy. | ||
- You shouldn't have more workers then you have cores on the computer it's running on. Some browsers have `navigator.hardwareConcurrency` and there is a [polyfill](https://github.com/oftn/core-estimator). | ||
- Workers are very good at speeding up operation which have a lot of computation and either a minimal amount of data in and out or that take buffers as input and output, for quick operation which involve a large amount of data, serializing and copying the data may take much longer then the actual | ||
- Latency and performance are not the same things. Doing an operation in a worker might cause the amount of time that the operation takes from start to finish to be longer, but since they are off the main thread, the biggest benefit of workers is not massively parallelized data processing (though you can sometimes do that), but moving things off of the main thread so that the page doesn't freeze. | ||
- Using array buffers which may be transfered |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
13277
10
238
1
59
2
2
1
+ Addedhbsify@^1.0.1
+ Addedlie@^2.7.7
+ Addedacorn@1.2.2(transitive)
+ Addedamdefine@1.0.1(transitive)
+ Addedast-types@0.8.15(transitive)
+ Addedasync@0.2.10(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedesmangle-evaluator@1.0.1(transitive)
+ Addedesprima-fb@15001.1001.0-dev-harmony-fb(transitive)
+ Addedfalafel@1.2.0(transitive)
+ Addedforeach@2.0.6(transitive)
+ Addedhandlebars@1.3.0(transitive)
+ Addedhbsify@1.0.1(transitive)
+ Addedimmediate@3.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinline-process-browser@1.0.0(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedlie@2.9.1(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedoptimist@0.3.7(transitive)
+ Addedprivate@0.1.8(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedrecast@0.10.43(transitive)
+ Addedsource-map@0.1.430.5.7(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough@2.3.8(transitive)
+ Addedthrough2@0.6.5(transitive)
+ Addeduglify-js@2.3.6(transitive)
+ Addedunreachable-branch-transform@0.2.3(transitive)
+ Addedwordwrap@0.0.3(transitive)
+ Addedxtend@4.0.2(transitive)