https://github.com/fawaz-ahmed/js-infinite-median
Calculate median of a stream of numbers using heap sort (with nlogn comlpexity)
https://github.com/fawaz-ahmed/js-infinite-median
crypto data-structures data-visualization graphs infinite math median median-heap numbers pricing streaming-data
Last synced: about 2 months ago
JSON representation
Calculate median of a stream of numbers using heap sort (with nlogn comlpexity)
- Host: GitHub
- URL: https://github.com/fawaz-ahmed/js-infinite-median
- Owner: fawaz-ahmed
- License: mit
- Created: 2022-06-15T08:45:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-06-21T11:18:25.000Z (over 3 years ago)
- Last Synced: 2025-06-16T04:19:12.148Z (4 months ago)
- Topics: crypto, data-structures, data-visualization, graphs, infinite, math, median, median-heap, numbers, pricing, streaming-data
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@fawazahmed/js-infinite-median
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
  
# 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
```javascript
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](https://stackoverflow.com/questions/10657503/find-running-median-from-a-stream-of-integers)
- Creators of this amazing [package](https://www.npmjs.com/package/@datastructures-js/heap) 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.