qsort-async

1.0.1 • Public • Published

Build Status Coverage Status

qsort-async

Async/Await style quicksort implementation in Javascript

Install

npm i qsort-async

Inspiration

While working with a rather large dataset, I noticed that a sort was blocking all I/O operations for my application. While this is fine for smaller arrays and datasets, I needed a sort that would yield to IO; specifically input from devices and web server requests.

Examples

For Numbers

const quicksort = require('qsort-async');
const arr = [3, 1, 2];
await quicksort(arr, (one, two) => one - two);
// arr [1, 2, 3];

For Strings

const quicksort = require('qsort-async');
const arr = ['c', 'a', 'b'];
await quicksort(arr, (one, two) => {
  if (one === two) {
    return 0;
  }
  return one > two ? 1 : -1;
});
// arr ['a', 'b', 'c'];

API

sort ( array, compare, [ size=10000 ] )

  • array - The array to sort in place
  • compare - function to compare elements of array
  • size - The size minimum at which setImmediate is skipped
    • if your compare function takes some time to execute, consider lowering this to yield to the eventloop/io more often
    • in theory, you could lower this all the way to 1, but this does not make sense for smaller arrays

TODO

Add a version that accepts an async comparator... but do you need it?

Package Sidebar

Install

npm i qsort-async

Weekly Downloads

43

Version

1.0.1

License

MIT

Unpacked Size

23.9 kB

Total Files

13

Last publish

Collaborators

  • mudrekh