Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compose-iterator

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compose-iterator

Fastest iterator for middleware composition

  • 1.0.0
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

Compose Iterator

Fastest iterator for middleware composition. The biggest performance improvements for Koa middleware.

Benchmarks

For koa-compose v3, https://github.com/koajs/compose/pull/57

Before

                      compose
          43,981 op/s » (fn * 1)
          25,044 op/s » (fn * 2)
          13,359 op/s » (fn * 4)
           6,972 op/s » (fn * 8)
           3,555 op/s » (fn * 16)
           1,803 op/s » (fn * 32)
             920 op/s » (fn * 64)
             466 op/s » (fn * 128)
             231 op/s » (fn * 256)
             113 op/s » (fn * 512)
              55 op/s » (fn * 1024)
              27 op/s » (fn * 2048)
              13 op/s » (fn * 4096)
               6 op/s » (fn * 8192)

After

                      compose
         206,669 op/s » (fn * 1)
         202,517 op/s » (fn * 2)
         209,224 op/s » (fn * 4)
         208,893 op/s » (fn * 8)
         205,962 op/s » (fn * 16)
         205,885 op/s » (fn * 32)
         205,316 op/s » (fn * 64)
         206,602 op/s » (fn * 128)
         205,352 op/s » (fn * 256)
         201,229 op/s » (fn * 512)
         188,328 op/s » (fn * 1024)
         167,314 op/s » (fn * 2048)
         182,743 op/s » (fn * 4096)
         174,105 op/s » (fn * 8192)

Installation

$ npm install compose-iterator

Examples

compose.js

'use strict'

const Promise = require('any-promise')

const iterator = require('compose-iterator')

/**
 * Expose compositor.
 */

module.exports = compose

/**
 * Compose `middleware` returning
 * a fully valid middleware comprised
 * of all those which are passed.
 *
 * @param {Array} middleware
 * @return {Function}
 * @api public
 */

function compose (middleware) {
  if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')
  for (const fn of middleware) {
    if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
  }

  /**
   * Make an iterator for middleware.
   */

  middleware[Symbol.iterator] = iterator

  // Alternative iteration.
  const iter = middleware[Symbol.iterator]()

  /**
   * @param {Object} context
   * @return {Promise}
   * @api public
   */

  return function (context, next) {
    try {
      return Promise.resolve(iter.next(context, next).value)
    } catch (err) {
      return Promise.reject(err)
    }
  }
}

app.js

const co = require('co')
const compose = require('./compose')

function wait (ms) {
  return new Promise((resolve) => setTimeout(resolve, ms || 1))
}

var arr = []
var stack = []

stack.push(function * (context, next) {
  arr.push(1)
  yield wait(1)
  yield next()
  yield wait(1)
  arr.push(6)
})

stack.push(function * (context, next) {
  arr.push(2)
  yield wait(1)
  yield next()
  yield wait(1)
  arr.push(5)
})

stack.push(function * (context, next) {
  arr.push(3)
  yield wait(1)
  yield next()
  yield wait(1)
  arr.push(4)
})

compose(stack.map((fn) => co.wrap(fn)))({}).then(function () {
  console.log(arr.toString() === [1, 2, 3, 4, 5, 6].toString())
})

Badges

Build Status


fundon.me  ·  GitHub @fundon  ·  Twitter @_fundon

Keywords

FAQs

Package last updated on 27 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc