npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

task.js_threads

v0.0.19

Published

Simplified interface for getting code to run on different cores (node.js, and web)

Downloads

12

Readme

task.js Build Status Code Climate

This modules is intended to make working with blocking tasks a bit easier, and is meant to work in node as well as the browser.

LIVE DEMO

This branch uses the webworker-threads module instead of forking

install

# node
npm install task.js_threads

or

# browser
bower install task

or just grab the cdnjs hosted version directly

important

Before using this module I want to expose the current performance issues

  • Node.js

    • When using task.js in node with very large messages it can be very slow, this is due to the fact that node is copying all the message data.
  • Clientside

    • When using task.js in a browser that doesn't support transferables (or you don't use them properly) then you will notice a slow down when passing massive array buffers

Rule of thumb in node keep your object size under 150kb, and in the clientside version you can go crazy and send 40MB array buffers if transferables are supported.

callbacks and promises

task.js supports both styles

var powTask = task.wrap(pow);

// callbacks
powTask(2, function (error, result) {

});

// promises
powTask(2).then(
	function (result) {},
	function (error) {}
);

task.defaults (optional)

You can override the defaults like this

// overriding defaults (optional)
var myCustomTask = task.defaults({
	debug: false, // extremely verbose, you should also set maxWorkers to 1
	logger: console.log, // (default: console.log, this is useful if you want to parse all log messages)
	warmStart: false, // (default: false, if set to true all workers will be initialized instantly)
	maxWorkers: 4, // (default: the system max, or 4 if it can't be resolved)
	idleTimeout: 10000, // (default: false)
	idleCheckInterval: 1000 // (default: null)
	globals: {}, // refer to globals information
	initialize: function (globals) {return globals;} // refer to globals information
});

behind the scenes it's spreading your dynamic work across your cores

task.wrap

You can wrap a function if the method signatures match, and it doesn't rely on any external variables.

// non async
function pow(number) {
	return Math.pow(number, 2);
}

var powTask = task.wrap(pow);
powTask(2).then(function (squaredNumber) {
	console.log(squaredNumber);
});

But keep in mind that your function cannot reference anything inside of your current scope because it is running inside of a worker.

task.run

Below is an example of using a transferable

var buffer = new ArrayBuffer();

task.run({
	arguments: [buffer, 1234],
	transferables: [buffer] // optional, and only supported client-side
	function: function (message) {
		return message.buffer;
	}
}).then(function (buffer) {
	console.log(buffer); // 4
});

task.terminate

When you run terminate it will destroy all current workers in the pool, and throw an error on all outstanding work.

task.terminate();

globals in workers

You can initialize a task instance to have predefined data from your main thread, or generated within the worker.

var data = {one: 1};

task.defaults({
	globals: {
		data: data,
		two: 2,
		three: 3
	}
})

task.wrap(function () {
	console.log(globals.data);
	console.log(globals.two);
	console.log(globals.three);
});

The above works great for small data, but with larger data this doesn't work. This is where you can use the initialize property in defaults.

task.defaults({
	globals: {
		data: {one: 1}
	},
	initialize: function (globals) {
		globals.bigData = [];
		for (var i = 0; i < 100000; i++) {
			globals.bigData.push(0);
		}
		return globals;
	}
});

You can also use initialize to define common methods in the worker scope as well.

task.defaults({
	globals: {
		data: {one: 1}
	},
	initialize: function (globals) {
		globals.myFunc = function () {
			...
		}

		return globals;
	}
});

Keep in mind that it is ok to have a slow initialize, no work will actually be processed until there is a fully initialized worker.

task.setGlobals

You can set globals across all of your workers like this

task.setGlobals({
	data: {one: 1}
});

The above will kill all current workers, create new ones, and then assign the new workers the outstanding tasks.

compatibility

Selenium Test Status