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

threadpool-js

v0.5.0

Published

General-purpose multi-threading library for javascript.

Downloads

29

Readme

threadpool.js Build Status npm version Bower version

threadpool.js is aimed to be a general-purpose multi-threading library for Javascript. Its key features are portability and ease of use. The library can either be used in a stand-alone fashion or as a require.js module.

Usage

You can add threadpool-js to your project using npm or bower:

npm install --save threadpool-js
# or
bower install --save threadpool-js

Or just by adding this script tag:

<script type="text/javascript" src="http://andywer.github.io/threadpool-js/dist/threadpool.min.js"></script>

Example use

Include the library at first. Just add the threadpool.js file to your project and include it per <script> tag. Alternatively you may use require.js or require it as a node.js module when using browserify or webpack.

// Init new threadpool with default size
var pool = new ThreadPool();

// Spawn two threads
pool
  .run(mythread, "Hello")
  .done(function(result) {
    document.write("Thread #1: " + result);
  });
pool
  .run(mythread, " World")
  .done(function(result) {
    document.write("Thread #2: " + result);
  });

// Hint: Keep in mind that you are free to use the done() and error() handlers
//       on single jobs and the whole pool!

pool.allDone(function() {
  document.write("All jobs are done.");
});

// Thread logic
function mythread (param, done) {
  done( param.toUpperCase() );
}

Running external scripts

You can also choose to run another javascript file instead of passing a function:

// Init new threadpool with default size
var pool = new ThreadPool();

// Spawn thread running another script file
pool
  .run("/path/to/script.js", { foo: 'bar' })
  .done(function(result) {
    console.log("Job finished and returned: ", result);
  });

Using libraries in the worker code

Assume that you want to use jQuery in your thread code. You cannot manipulate the DOM from there, but you might need some convenience methods. You can import other Javascript files into the scope of your thread code like this:

pool.run(["/path/to/jQuery.min.js"], function(param, done) { /* do something awesome */ });

Support for transferable objects

If you want to pass large blobs to your workers efficiently, you may use a feature called transferable objects.

threadpool-js supports them. Just pass the array of buffers to transfer (after the worker parameter) to the pool's run method:

pool.run(mythread, {hash: "sha512", data: myUint8Array}, [myUint8Array.buffer]);

Demo

Try the samples.

(Use Chrome, Firefox, IE, or Opera)

Note: IE support experimental

License

This library is published under the MIT license. See LICENSE for details.