poker-bitvaljs
v1.2.0
Published
High-performance poker hand evaluator optimized for browser environments, using bitwise operations for rapid poker hand analysis and equity calculations.
Maintainers
Readme
BitVal
High-performance poker hand evaluator optimized for browser environments, using bitwise operations for rapid poker hand analysis and equity calculations.
🌐 Try it online - Range vs Range Evaluator
Features
- Fast hand evaluation using bitwise operations
- Range vs range equity calculations with canonical key caching optimization
- Web Workers support for parallel processing across multiple CPU cores (3-7x speedup)
- Monte Carlo simulation for preflop and postflop scenarios
- Exhaustive enumeration for flop/turn scenarios (2 or fewer cards to come)
- Progress callbacks with configurable update intervals
- Browser and Node.js compatible
Installation
npm install poker-bitvaljsUsage
Basic Hand Evaluation
const BitVal = require('poker-bitvaljs');
const bitval = new BitVal();
// Evaluate a hand (7 cards: 2 hole cards + 5 board cards)
const handMask = bitval.getBitMasked(['As', 'Ah', 'Ks', 'Qs', 'Js', 'Ts', '9s']);
const [evaluation, kickers] = bitval.evaluate(handMask);
// Returns [evaluation_score, kickers]
// Higher evaluation score = stronger handRange vs Range Comparison
const BitVal = require('poker-bitvaljs');
const bitval = new BitVal();
// Compare two ranges
const heroHands = ['AsAh', 'AsAd', 'AsAc', 'AhAd', 'AhAc', 'AdAc']; // All AA combinations
const villainHands = ['KsKh', 'KsKd', 'KsKc', 'KhKd', 'KhKc', 'KdKc']; // All KK combinations
const result = await bitval.compareRange(
heroHands,
villainHands,
[], // board cards (empty for preflop)
[], // dead cards
5, // number of board cards
10000, // iterations
true, // optimize (use canonical caching)
null, // progress callback (optional)
100, // progress interval (optional, default: 100)
true // use workers (optional, default: true)
);
// Result: { win: 8120, tie: 30, lose: 1850 }
// Calculate equity: (win + tie/2) / (win + tie + lose) * 100With Progress Callback
const result = await bitval.compareRange(
heroHands,
villainHands,
[],
[],
5,
100000,
true,
(current, total, message) => {
console.log(`${Math.round((current/total)*100)}% - ${message}`);
},
100, // Update progress every 100 matchups
true // Use Web Workers for parallelization
);With Web Workers Disabled
// Disable Web Workers (useful for debugging or when workers aren't supported)
const result = await bitval.compareRange(
heroHands,
villainHands,
[],
[],
5,
10000,
true,
null,
100,
false // Disable Web Workers, use sequential execution
);With Board Cards (Postflop)
// Compare ranges on a flop
const boardCards = ['As', 'Ks', 'Qs'];
const result = await bitval.compareRange(
heroHands,
villainHands,
boardCards,
[],
5,
10000,
true
);API
new BitVal()
Creates a new BitVal instance.
evaluate(handMask)
Evaluates a poker hand represented as a BigInt bitmask.
Parameters:
handMask(BigInt): Bitmask representing 5-7 cards
Returns:
[evaluation, kickers](Array): Evaluation score and kicker bits
getBitMasked(cards)
Converts an array of card strings to a BigInt bitmask.
Parameters:
cards(Array): Array of card strings (e.g.,['As', 'Kh', 'Qd'])
Returns:
BigInt: Bitmask representing the cards
simulate(iterations, numberOfBoardCards, hero, villain, board, deadCards)
Simulates a single hand vs hand matchup.
Parameters:
iterations(Number): Number of simulationsnumberOfBoardCards(Number): Total board cards (default: 5)hero(Array): Hero's hole cards as bitmaskvillain(Array): Villain's hole cards as bitmaskboard(Array): Board cards as bitmaskdeadCards(Array): Dead cards as bitmask
Returns:
{ win, tie, lose }: Results object
compareRange(heroHands, villainHands, boardCards, deadCards, numberOfBoardCards, iterations, optimize, progressCallback, progressInterval, useWorkers)
Compares two ranges of hands with optional optimization and Web Workers parallelization.
Parameters:
heroHands(Array): Array of hero hand strings (e.g.,['AsAh', 'AsAd'])villainHands(Array): Array of villain hand stringsboardCards(Array): Board cards as strings (default:[])deadCards(Array): Dead cards as strings (default:[])numberOfBoardCards(Number): Total board cards (default:5)iterations(Number): Number of simulations per matchup (default:10000)optimize(Boolean): Use canonical key caching for performance (default:true). Note: Optimizations may result in a ±0.5% margin of error compared to unoptimized calculations.progressCallback(Function): Optional callback(current, total, message) => {}(default:null)progressInterval(Number): Update progress callback every N matchups (default:100)useWorkers(Boolean): Use Web Workers for parallelization across CPU cores (default:true). Automatically falls back to sequential execution if workers are unavailable or workload is too small (< 4 matchups).
Returns:
Promise<{ win, tie, lose }>: Results object
Web Workers Notes:
- Web Workers provide 3-7x speedup on multi-core systems by parallelizing matchup evaluations
- Workers are automatically disabled when:
useWorkersisfalse- Workers are not supported by the browser
- There are fewer than 4 matchups (overhead not worth it)
- For local testing with
file://protocol, use a local web server (see Testing Locally)
Performance
- Optimized for browser environments with efficient bitwise operations
- Web Workers parallelization provides 3-7x speedup on multi-core systems
- Canonical key caching reduces redundant evaluations (may introduce ±0.5% margin of error)
- Exhaustive enumeration for flop/turn (2 or fewer cards to come)
- Monte Carlo simulation for preflop and river scenarios
- Adaptive progress reporting to minimize UI blocking
Test performance and benchmark online: https://darraghgeo.github.io/poker-bitvaljs/
Performance Tips
- Enable Web Workers (default) for best performance on multi-core systems
- Use canonical caching (
optimize: true) for large range comparisons - Adjust
progressIntervalto balance UI responsiveness vs. performance - For very small workloads (< 4 matchups), workers are automatically disabled to avoid overhead
Testing Locally
When testing locally, Web Workers require a proper HTTP origin. If you open index.html directly from the file system (file:// protocol), workers will fail with a SecurityError.
Solution: Use a local web server
# Python 3
python3 -m http.server 8000
# Node.js (if you have npx)
npx http-server -p 8000
# PHP (if installed)
php -S localhost:8000Then open http://localhost:8000 in your browser. The application will automatically fall back to sequential execution if workers are unavailable.
License
MIT
Copyright (c) 2024
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
