New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

iterative-permutation

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iterative-permutation

Fast, iterative implementation of Heap's Algorithm

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

iterative-permutation

Fast, iterative implementation of Heap's Algorithm for JavaScript

Overview

This is a non-recursive implementation of Heap's Algorithm that can be used to generate permutations for very large sets of values. Typically recursive solutions generate all sets of values and then return them all at the end. For very large sets it becomes problematic to store all of the permutations in memory. Instead this implementation returns permutations one at a time so they can be processed and discarded if needed.

Installation

npm install iterative-permutation

Example

var Permutation = require('iterative-permutation');

var generator = new Permutation([1, 2, 3]);
while (generator.hasNext()) {
  console.log(generator.next());
}

console.log('finished');

Prints:

[ 1, 2, 3 ]
[ 2, 1, 3 ]
[ 3, 1, 2 ]
[ 1, 3, 2 ]
[ 2, 3, 1 ]
[ 3, 2, 1 ]
finished

Implementation Notes

This implementation keeps track of the algorithm stack in a variable called stack and then updates it with the current index each time a new permutation is calculated. It only uses for loops and simple function calls and has no external dependencies, just regular ol' JavaScript. Why no ES6 generators? Some people have found that generators are a little slow and typically when you need a large number of permutations it's in a hot code path. This implementation tries to be as fast as reasonably possible.

Author

Brian Card: @bmcard

License

MIT License - Copyright 2015 Brian Card

Keywords

FAQs

Package last updated on 16 Aug 2015

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