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

unjank

v1.0.4

Published

Do things without locking up the UI

Downloads

11

Readme

Unjank

Build Status

unjank is an asynchronous Array.prototype.map that doesn't lock up the browser's UI.

  • Quickly learns how expensive it is to perform each task
  • Runs the task in batches to acheive a target FPS
  • Allows you to abort at any time
// Simulate an expensive function that takes 4ms to execute
function expensiveFunction (t, cb) {
  setTimeout(function () {
    cb(null, t * 10)
  }, 4)
}

// Will only run expensiveFunction five times per frame to acheive 30 FPS
unjank([1,2,3,4,5,6,7,8,9,10], expensiveFunction, {targetFPS: 30}, function (err, results) {
  // results => [10, 20, 30, ...]
})

API

unjank(data, map, [opts], cb)

  • data must be an array
  • map can either be:
    • function sync (item) { return transform(item) }
    • function batchSync (batch) { return batch.map(transform) }
    • function async (item, cb) { cb(null, transform(item)) }
    • function batchAsync (batch, cb) { cb(null, batch.map(transform) }
  • opts is an optional object
    • opts.targetFPS defaults to 30
    • opts.batchMap defaults to false
  • cb should have the signature function cb(err, results, metadata) {}
    • err if an async map function returns an error, this is where it goes
    • results an array, just what you would expect from array.map
    • metadata information learned by unjank during execution
      • metadata.intervalPerItem The average number of milliseconds each map(item) took
      • metadata.batchSize The optimal number of items mapped per frame

Return Value

unjank returns an instance object.

  • instance.completed is true if the operation completed (and was not aborted)
  • instance.aborted is true if the operation was aborted
  • instance.abort is a function you can call to abort the operation

Aborting

You can abort the task at any time by calling abort() on the returned object.

var instance = unjank(data, map, cb)
instance.abort()

This will cause the callback function to be called with new Error('Aborted').

You cannot abort a task more than once, or once it has completed.

Async Example

var unjank = require('unjank')
  , data = [1, 2, 3, 4, 5, 6]
  , asyncMap = function (item, cb) {
      setTimeout(function () {
        cb(null, item * 10)
      }, 4)
    }

// Ensure that each batch takes no longer than 32 ms to execute
// in order to achieve 30FPS
unjank(data, asyncMap, function (err, results, metadata) {
  // metadata -> {intervalPerItem: 4, batchSize: 4}
})

Sync Example

var unjank = require('unjank')
  , data = [1, 2, 3, 4, 5, 6]
  , syncMap = function (item) {
      return item * 10
    }

// Ensure that each batch takes no longer than 16.6 ms to execute
// in order to achieve 60FPS
unjank(data, syncMap, {targetFPS: 60}, function (err, results, metadata) {
  // your code here
})

Batch Mapping Example

Sometimes your task is best handled as a batch, instead of individually.

For example, you might want to render many Backbone views at the same time, but only append them to the DOM as a single DocumentFragment. This is a very fast way to render a large collection.

With the batchMap option set to true, unjank will call your map function once per batch instead of once per item.

var unjank = require('unjank')
  , async = require('async')
  , data = [1, 2, 3, 4, 5, 6]
  , mapf = function (t, cb) { cb(null, t * 10)}
  , batchMap = function (batch, cb) {
      async.map(batch, mapf, function (err, results) {
        cb(null, results.reduce(function sum (a, b) { return a + b}))
      })
    }

unjank(data, batchMap, {batchMap: true}, function (err, results, metadata) {
  // your code here
})