@kanaries/ml
Guides

JS ML Libraries vs TensorFlow.js & Pyodide

Compare @kanaries/ml, TensorFlow.js, scikit.js, and Pyodide for browser and Node.js ML across bundle size, classical tabular algorithms, and edge runtimes.

View as Markdown

Why choosing the right JavaScript ML runtime matters

When engineering teams decide to move machine learning out of a Python microservice and into a browser tab, Web Worker, Node.js API, or edge runtime, they quickly discover that "JavaScript ML" is not a single category. A library built for GPU tensor graphs solves a very different problem from a library built for classical tabular models, or a WebAssembly port of the entire CPython scientific stack.

Picking the wrong runtime usually leads to one of three costly mistakes:

  • Shipping a 15–40 MB WebAssembly Python runtime into a browser just to run K-Means, PCA, or Logistic Regression on a 500-row CSV.
  • Trying to hand-build decision trees, Isolation Forest, DBSCAN, or TF-IDF pipelines on top of a neural-network tensor framework that was never designed for tree splits or sparse CSR text matrices.
  • Relying on native C++ Node.js bindings (node-gyp) that fail to install on ARM laptops, break in Cloudflare Workers / Vercel Edge, and cannot run in the browser at all.

This guide gives a candid, engineering-level comparison of @kanaries/ml, TensorFlow.js, scikit.js, and Pyodide so you can pick the right tool—or combine them cleanly.

Side-by-side comparison matrix

Dimension@kanaries/mlTensorFlow.js (@tensorflow/tfjs)scikitjsPyodide (CPython + scikit-learn in WASM)
Primary strengthClassical tabular ML, clustering, anomaly detection, text TF-IDF, pipelinesDeep learning, CNNs, embeddings, WebGL/WebGPU tensor opsThin TensorFlow.js wrapper over a subset of sklearn estimatorsFull Python scikit-learn / numpy / scipy compatibility in browser
Runtime architecturePure TypeScript, zero native binariesJS + WebGL / WebGPU / WASM / native C++ (tfjs-node)Requires TensorFlow.js tensor backendFull WebAssembly CPython interpreter + wheel loader
Typical cold-start & payloadTens to low hundreds of KB (tree-shakeable ESM)~1–4 MB JS + backend shaders/WASMTF.js payload + wrapper overhead~15–40+ MB WASM + numpy/scipy/scikit-learn wheels; multi-second startup
Edge runtimes (Cloudflare Workers / Vercel Edge)Runs natively (pure JS/TS, no native addons)Limited (no WebGL/WebGPU; CPU backend or custom WASM only)Limited by TF.js backend constraintsImpractical for standard edge memory & cold-start budgets
Classical estimators (Trees, Forests, SVM, GMM, HDBSCAN, IsolationForest, LOF, LDA/QDA)Broad native coverage (35+ modules)Not built-in (neural layers & tensors only)Partial subset; maintenance lags behind core sklearnComplete scikit-learn coverage
Composable Pipeline & ColumnTransformerNative (Pipeline, ColumnTransformer, FeatureUnion)Keras Sequential/Functional layers (tensors, not tabular pipelines)PartialComplete scikit-learn pipelines
Model persistenceBuilt-in portable JSON (toJSON() / loadModel())model.json + binary weight shardsTF.js / custom statepickle / joblib inside WASM virtual filesystem
Best hardware acceleratorSingle-threaded JS / Web WorkersGPU (WebGL / WebGPU / CUDA in Node)TensorFlow.js backendSingle-threaded WASM CPU

When to choose each library

1. Choose @kanaries/ml for classical ML in browser, Node.js, and Edge

@kanaries/ml is purpose-built for classical tabular, clustering, anomaly-detection, dimensionality-reduction, and text-vectorization workflows in pure TypeScript with a scikit-learn-like API.

Choose @kanaries/ml when you need:

2. Choose TensorFlow.js (or ONNX Runtime Web) for neural networks & GPU inference

If your problem involves vision (CNNs), audio spectrograms, transformer embeddings, or custom autodiff neural networks, GPU matrix multiplication is non-negotiable. TensorFlow.js (with WebGL/WebGPU backends) or onnxruntime-web will outperform pure-JavaScript CPU loops by orders of magnitude on dense tensor workloads.

A very common hybrid architecture uses TensorFlow.js / ONNX to compute dense embeddings and @kanaries/ml to run fast downstream clustering (Clusters.EMeans, Clusters.HDBSCAN), projection (Decomposition.PCA), or anomaly scoring (Ensemble.IsolationForest) over those embeddings right in the browser.

3. Choose scikitjs only if you are already standardized on TensorFlow.js tensors

scikitjs wraps TensorFlow.js tensors with familiar scikit-learn method names. If your pipeline already lives exclusively inside tf.Tensor objects and you only need a small subset of basic estimators, it can bridge the gap. However, carrying the full TensorFlow.js runtime (and manual tensor memory disposal via tf.tidy()) is unnecessary overhead for plain number[][] tabular arrays, tree ensembles, sparse text, or serverless edge functions.

4. Choose Pyodide when you need verbatim Python notebooks in the browser

If you are building an in-browser Jupyter notebook (like JupyterLite), teaching a Python data-science course, or executing arbitrary user-authored Python scripts that rely on obscure scipy.stats or scikit-learn internals, Pyodide is unmatched. The tradeoff is UX and operational footprint: downloading 20–40 MB of WebAssembly wheels and booting CPython takes seconds—acceptable in an IDE/notebook tab, unacceptable for a responsive consumer SaaS widget or edge middleware.

Concrete workflow comparison: tabular pipeline in TypeScript

Notice how clean a complete preprocessing + model + JSON serialization flow looks with @kanaries/ml compared to maintaining a separate Python service or managing GPU tensor lifecycles:

import { Pipeline, loadModel, Linear, utils, Metrics } from '@kanaries/ml';

const XTrain = [[1, 100], [2, 110], [8, 20], [9, 15], [1.5, 105], [8.5, 18]];
const yTrain = [0, 0, 1, 1, 0, 1];

const pipe = new Pipeline({
  steps: [
    ['scaler', new utils.Preprocessing.StandardScaler()],
    ['clf', new Linear.LogisticRegression({ maxIter: 200 })],
  ],
});

pipe.fit(XTrain, yTrain);

// Portable JSON artifact — works in localStorage, KV, S3, or Edge bundles
const artifact = JSON.stringify(pipe);
const restored = loadModel(artifact) as typeof pipe;

const preds = restored.predict([[1.8, 108], [8.2, 19]]);
console.log({ preds, accuracy: Metrics.accuracyScore([0, 1], preds) });

Summary decision checklist

  1. Deep learning / vision / GPU tensors? → TensorFlow.js or ONNX Runtime Web.
  2. In-browser Python IDE / exact CPython scientific stack? → Pyodide.
  3. Classical tabular ML, clustering, anomaly detection, TF-IDF, or lightweight Edge/Web Worker inference in TypeScript? → @kanaries/ml.

Next steps