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

nb-split-tasks

v0.6.0

Published

Simple split tasks then combine result together for node or browser

Downloads

13

Readme

nb-split-tasks

simple tool to split tasks then combine the result together for node or browser

This module is develop for the jsonql (especially in the contract-cli)

Notes

It only supports node.js at the moment (browser support coming soon)

As of 0.4.0 release, it support ES6 module, such as:

export default function add(x, y) {
  return x + y;
}

Please note, we are using the esm module to do the ES to CJS on the fly.

Checking your CPU before it runs

There is a little check to see how many CPU you have got, and the slots is number of cpu - 1 if it returns zero, that means you only have one processor (which is highly unlikely this day). Then it will return a rejected promise. You can check it like this:

const { NOT_ENOUGH_CPU } = require('nb-split-tasks/constants')
const nbSplitTasks = require('nb-split-tasks')

nbSplitTasks(pathToFn, args)
  .then(result => {
    // do your thing with the result
  })
  .catch(err => {
    if (err === NOT_ENOUGH_CPU) {
      // do alternative processing?
    }
  })

Too many file access might hang your machine

Internally, it's using the fork method to include your external file pathToFn; every time it split the task, it creates an entirely new instance of node.js, and I found if I try to access files from too many split tasks, it could hang the machine for a little while, therefore, use this with caution, even though my dev machine is core quad AMD with NVME SSD.

Examples

Just learn by examples

Return as an Array

By default it will merge the result as one object, or you can pass array as the third parameters then each result will put into an array

If you only have one function to call:

// fn.js
module.exports = function(x, y) {
  return x + y
}

Then


nbSplitTasks('/path/to/fn.js', args, 'array')
  .then(result => {
    // the return result will be
    // [3, 7, 11]
  })

If you have multiple functions then you can setup an array that is the same length as the arguments:

const nbSplitTasks = require('nb-split-tasks')

const pathToFns = [
  '/path/to/fn1.js',
  '/path/to/fn2.js',
  '/path/to/fn3.js'
] // or you can call the same methods with

const args = [
  [1,2],
  [3,4],
  [5,6]
]

// The function in each of the files
function fn1(x, y) {
  return x + y
}
function fn2(x, y) {
  return y - x;
}
function fn3(x, y) {
  return x * y
}
// don't forget to export that function as the default export

nbSplitTasks(pathToFns, args, 'array')
  .then(result => {
    // the return result will be
    // [3, 1, 30]
  })

Return as an Object

This is the default, and the function will try to merge all the result into one object; or you can pass an plain object with pre-defined properties, and let the result merge into it.


nbSplitTasks('/path/to/processor.js', args, {someKey: 'some init value'})
  .then(result => {
    assert(result.someKey, 'some init value')
  })

Cool little trick

This is from a live module I publish else where (not the exact code, but the idea is the same) If you only want to split the task out to another process, and don't want to create too many files. All you need is two

// the main.js
const nbSplitTasks = require('nb-split-tasks')

function doCal(x, y) {
  return x + y
}

function doExtralCal(value) {
  return value * 100
}

function callDoCal(x, y) {
  const pathToFn = './sub.js'
  return nbSplitTasks(pathToFn, [[x, y]]) // note array of array!
    .then(result => {
      // do a bit post processing if you want
      return result[0] // there is only result anyway
    })
}

module.exports = {
  doCal,
  doExtralCal,
  callDoCal
}

Next create the file for the callDoCal to use:

// sub.js
const { doCal, doExtralCal } = require('./main')
// of course this is overly simply in fact, the original
// main.js has several more methods that combine into one method
module.exports = function(x, y) {
  const result = doCal(x, y)
  return doExtralCal(result)
}

Then when you need it:

// app.js
const { callDoCal } = require('./main')

const result = callDoCal(1, 3)

Joel Zhu 2019 ISC