fx42-node
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

fx42-node

MEAN Module Build Status npm version Node.js Version

Sorted array in JavaScript.

  • No extra classes.
  • No extending prototypes.
  • TypeScript friendly (comes with TypeScript type definitions).

Installation

yarn add fx42-node

Example

const { insertToSortedArray } = require('fx42-node');

// By default, `array.sort` will sort things alphabetically, we need to specify a `compareFn` for sorting numbers
const comparer = (a, b) => {
  return a - b;
};

// Sort the array first
const array = [1, 10, 3, 5, -2];
array.sort(comparer);

// Insert values to the sorted array
for (let i = 0; i < 10; i++) {
  insertToSortedArray(array, i, comparer);
}

console.log(array);
// Prints [ -2, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9, 10 ]

Benchmarks

To run benchmarks:

yarn run benchmark

Possible result:

Push and sort x 4,879 ops/sec ±37.27% (12 runs sampled)
Sorted array x 11,381 ops/sec ±74.13% (6 runs sampled)
Fastest is Sorted array

This sorted array implementation is around 3x faster then push and resort the array.

Readme

Keywords

Package Sidebar

Install

npm i fx42-node

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

6.34 kB

Total Files

8

Last publish

Collaborators

  • mgenware