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

bizon

v1.0.2

Published

The simple library to run functions in a worker thread.

Downloads

20

Readme

Bizon

Bizon is a simple library that allows you to run functions in a worker thread of Node.JS.

Installation

npm i --save bizon

Basic usage

const { function$ } = require('bizon');


void async function() {
  // Initialize your own thread function
  const multiply$ = function$((a, b) => {
    return a + b;
  });
  
  // Then call the function and get a result. Your thread function returns a promise
  const result = await multiply$(10, 20);
  console.log(result); // 30
}();

Using imports

const { function$ } = require('bizon');
const fs = require('fs');

void async function() {
  // You can use required modules of the parent thread
  const readFile$ = function$((pathToFile) => {
    return fs.readFileSync(pathToFile).toString();
  });
  
  // The function will be async, because it will be run in a thread
  const result = await readFile$('./index.js');
  console.log(result);
}();

'this' in a thread function

You can use 'this' context

const { function$ } = require('bizon');

class Person {
  name = null;
  
  setName$ = function$((newName) => {
    // You can get access to 'this' context inside your thread function!
    // But remember! You can not use methods of 'this' inside a thread function
    this.name = newName;
  });
}

void async function() {
  const person = new Person();

  console.log(person.name); // null
  await person.setName$('Bob');
  console.log(person.name); // Bob
}();

Arguments

You can pass numbers, strings, arrays, objects and functions to the arguments

const { function$ } = require('bizon');

void async function() {
  // You can create the thread map!
  // You can pass a callbacks to arguments of a thread function
  Array.prototype.map$ = function$((cb) => {
    // 'this' will be specify to an array
    // Just call map in thread function and pass callback
    return this.map(cb);
  });
  
  const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

  // Just call map$ like the sync map function of an array
  const result = await numbers.map$((val, i) => {
    return val ** i;
  });
  
  console.log(result);
}();

Recursive calls

You can call your function recursively

const { function$ } = require('bizon');

void async function () {
  // For recursively call you need to pass named function expression as callback
  const fib$ = function$(function fib(n) {
    return n <= 1 ? n : fib(n - 1) + fib(n - 2);
  });
  
  const result = await fib$(40);
  console.log(result) // 102334155
}();

Restrictions

You can not get access to variables defined outside the function

const { function$ } = require('bizon');

const a = 10;

const func$ = function$(() => {
  return a + 5;
});

func$().then(result => console.log(result));

// Error: ReferenceError: a is not defined

You can not return objects and arrays with promises and functions

const { function$ } = require('bizon');

const func$ = function$(() => {
    return {
      foo: new Promise((resolve) => resolve('hello')),
    };
});

func$().then(result => console.log(result));

//  Error: DataCloneError: #<Promise> could not be cloned.

You can not return functions

const { function$ } = require('bizon');

const func$ = function$(() => {
    return function() {
      return 'hello';
    }
});
func$().then(result => console.log(result));

// Error: DataCloneError: function () {
//   return 'hello';
// } could not be cloned.