a small, fast and easy to read price-time priority (fifo) matching engine written in typescript. good for learning, and a solid base for a cex or an off-chain order book of a dex.
- price priority: best price goes first (highest bid, lowest ask).
- time priority: at the same price, the order that came first gets filled first.
- trades always happen at the maker's price (the order already resting in the book).
- limit and market orders
- partial fills, cancel, depth snapshot
- multi-market support (
btc-usdt,eth-usdt, ...) - deterministic: uses a sequence counter, not
date.now(), so same input = same output (great for replay and dex sequencers) - zero runtime dependencies
npm install
npm test
npm run example- use integers for price and quantity (ticks and lots). floats cause rounding bugs in money code.
example: price
60000.50with tick size0.01becomes6000050. - single-threaded by design. run one book per market and process orders one by one.
| action | cost |
|---|---|
| best bid / ask | O(1) |
| cancel order | O(1) + O(log n) only if level is empty |
| add order (new) | O(log n) search + array shift |
| add order (old) | O(1) |
| match one fill | O(1) |
n = number of price levels (not orders).


