New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

web-worker

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web-worker - npm Package Compare versions

Comparing version 0.0.0 to 0.1.0

.travis.yml

21

package.json
{
"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 [![Build Status](https://travis-ci.org/calvinmetcalf/web-worker.svg)](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
index.js
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc