Creating a Lightning‑Fast Zero‑Dependency JavaScript Mahjong AI for the Browser

Neptune Infotech Team
Neptune Infotech Team
|
December 04, 2026
Creating a Lightning‑Fast Zero‑Dependency JavaScript Mahjong AI for the Browser

Mahjong enthusiasts often struggle to find a lightweight, browser‑based AI that supports multiple rule sets without the bloat of heavy frameworks or mandatory sign‑ups.

Zero‑dependency pure‑JavaScript Mahjong AI refers to a self‑contained engine written entirely in vanilla JavaScript that runs in the browser without external libraries.

Why a Zero‑Dependency Approach Matters

Using only native JavaScript reduces load time, eliminates version conflicts, and ensures the AI can be embedded on any static site or progressive web app. This simplicity also makes the codebase easier to audit, extend, and maintain.

Core Architecture of the Mahjong AI

The engine is split into three logical layers:

  • Tile Engine: Handles tile representation, shuffling, and draw logic for all supported rule sets.
  • Shanten Calculator: Determines the distance to a winning hand using combinatorial analysis.
  • Target‑Route Engine: Generates optimal discard and draw recommendations based on the current shanten value and opponent visibility.

Implementing Shanten Calculation in Pure JavaScript

Shanten is the minimum number of tile changes needed to complete a hand. The algorithm follows these steps:

  1. Group tiles into melds (sequences, triplets, pairs).
  2. Count incomplete groups and isolated tiles.
  3. Apply rule‑specific adjustments (e.g., honor tiles in Hong Kong Mahjong).

Because the calculation is performed on arrays of integers, it runs in O(n) time where n is the number of tiles, keeping the CPU footprint minimal.

Building the Target‑Route Engine for Optimal Play

The engine evaluates every possible discard, simulates the next draw, and scores the resulting hand using the shanten value and estimated opponent risk. A depth‑first search with pruning ensures that only the most promising branches are explored, delivering real‑time suggestions without noticeable lag.

Performance Optimizations to Achieve 416 ms LCP

Key techniques used to keep the Largest Contentful Paint under half a second include:

  • Lazy‑loading the AI script only when the game canvas becomes visible.
  • Minifying the source with terser and serving it via HTTP/2 push.
  • Using Web Workers to offload heavy shanten calculations from the main thread.

These practices collectively reduce the initial payload to under 30 KB and keep the UI responsive on low‑end devices.

Frequently Asked Questions

Do I need any external libraries to run this AI?

No. The entire engine is written in vanilla JavaScript and works in any modern browser.

Can the AI support rule sets beyond Japanese Riichi?

Yes. The tile engine and shanten calculator are modular, allowing you to plug in Hong Kong, Chinese Official, Sichuan, or American Mahjong rules.

How does the AI handle network latency on mobile devices?

All calculations run locally in the browser or a Web Worker, so network latency does not affect decision‑making speed.

Is the source code open for customization?

Absolutely. The project is released under an MIT license, encouraging developers to extend or adapt the logic.

What browsers are supported?

Any browser that implements ES6+, including Chrome, Edge, Firefox, Safari, and mobile browsers on Android and iOS.

Neptune Infotech can help you integrate this AI into custom web or mobile solutions, delivering fast, reliable experiences for your users.

You Might Also Like

Explore more articles related to "Web Development"

Revolutionizing Image Editing: Building a Browser‑Based Prompt AI Photo Editor with Unlimited Undo

Revolutionizing Image Editing: Building a Browser‑Based Prompt AI Photo Editor with Unlimited Undo

Editing a product photo or portrait often means launching a heavyweight desktop suite, wrestling wit...

How Automattic’s Purchase of Miles Is Redefining AI‑Powered WordPress Design

How Automattic’s Purchase of Miles Is Redefining AI‑Powered WordPress Design

Artificial intelligence is rapidly reshaping how websites are built, and the latest move by WordPres...

Understanding CSS System Colors: Modern Usage and Browser Support

Understanding CSS System Colors: Modern Usage and Browser Support

System colors in CSS let developers tap into the operating system’s native palette, ensuring that we...