@fawazahmed/js-infinite-median
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

NPM Downloads NPM License NPM Version

js-infinite-median

TS/JS compatible

Calculate median of a stream of numbers using heap sort (with nlogn comlpexity)

Installation

npm i @fawazahmed/js-infinite-median --save

or with yarn

yarn add @fawazahmed/js-infinite-median

Usage

import { InfiniteMedian } from '@fawazahmed/js-infinite-median';
// Using require:
// const { InfiniteMedian } = require("@fawazahmed/js-infinite-median")

// Create an instance of InfiniteMedian class
const instance = new InfiniteMedian()

// keep adding numbers from stream
instance.insert(1)
instance.insert(2)
instance.insert(3)
instance.insert(4)
instance.insert(5)

// fetch the median from instance
const median = instance.getMedian()
// median has a value 5

Credits

  • I would like to thank members of stackoverflow who posted relevant answers on this thread
  • Creators of this amazing package for heap algo

Applications

  • Typically to fetch the median price of any stock/crypto/fiat or any other trades out there, this lib can be used, where you get stream of data and process in real time.

Drawbacks

  • Since the instance remains in memory, eventually the data will outgrow and crash the app due to memory overflow. To overcome this, there can be another wrapper or some code modification to remove a subset of data from memory periodically.

Package Sidebar

Install

npm i @fawazahmed/js-infinite-median

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

8.15 kB

Total Files

9

Last publish

Collaborators

  • fawaz-ahmed