TensorFlow.js
Last reviewed
Apr 30, 2026
Sources
No citations yet
Review status
Needs citations
Revision
v1 ยท 3,845 words
Improve this article
Add missing citations, update stale details, or suggest a clearer explanation.
Last reviewed
Apr 30, 2026
Sources
No citations yet
Review status
Needs citations
Revision
v1 ยท 3,845 words
Add missing citations, update stale details, or suggest a clearer explanation.
TensorFlow.js (often abbreviated TFJS) is an open-source JavaScript library developed by Google for training and running machine-learning models directly in the web browser, in Node.js, and on mobile through React Native. It is a member of the broader TensorFlow ecosystem and provides JavaScript-native APIs that mirror the Python TensorFlow and Keras APIs. Because computation runs on the user's device, TensorFlow.js eliminates server round-trips, keeps user data local, and lowers inference latency for interactive applications such as webcam pose tracking, browser games, and on-device personalisation.
The library was officially announced on March 30, 2018 at the TensorFlow Developer Summit, where Google demonstrated a webcam-controlled PAC-MAN trained entirely in the browser. It is the successor to deeplearn.js, an earlier WebGL-accelerated JavaScript library released in August 2017 by the Google PAIR (People + AI Research) team. TensorFlow.js is distributed under the Apache 2.0 license and is published as the npm package @tensorflow/tfjs, with separate packages for individual backends, the model converter, the layers API, and platform adapters.
TensorFlow.js is a library for defining tensors, building neural networks, training them with automatic differentiation, and running inference in JavaScript runtimes. Its design goals, set out in the foundational MLSys 2019 paper by Smilkov et al., are: full GPU acceleration via the web stack, parity with the Python TensorFlow API where reasonable, the ability to import models trained in Python or other tools, and a compact bundle suitable for shipping over the web.
The library targets four main runtimes:
expo-gl adapter exposes the device GPU.In each case the developer writes the same TensorFlow.js code; the library selects the fastest available backend at startup.
The direct predecessor of TensorFlow.js was deeplearn.js, a WebGL-accelerated JavaScript library announced by Google researchers Daniel Smilkov and Nikhil Thorat on August 11, 2017. Smilkov and Thorat were members of Google's PAIR (People + AI Research) initiative, which had previously produced the TensorFlow Playground, an interactive in-browser visualisation of small neural networks. Deeplearn.js generalised the techniques used in the Playground into a full library: tensors stored as WebGL textures, fragment shaders implementing element-wise and matrix operations, and a NumPy-style API with eager execution and automatic differentiation.
Deeplearn.js was aimed at visual demos and education. Early projects shipped in deeplearn.js included style transfer, image-to-image translation, and an in-browser implementation of SqueezeNet. Although the library was popular, it lacked official ties to TensorFlow's Python ecosystem, and its API diverged from the Keras conventions familiar to most ML practitioners.
On March 30, 2018, Google relaunched deeplearn.js as TensorFlow.js at the TensorFlow Developer Summit. The relaunch added a Keras-compatible Layers API on top of the existing low-level Core API, plus a converter that imports TensorFlow SavedModel and Keras HDF5 files, and renamed deeplearn.js itself to TensorFlow.js Core. The summit talk by Smilkov and Thorat featured a live demo of training a PAC-MAN controller from a webcam feed, showing that the browser could now do real backpropagation, not just inference.
The launch also introduced the Emoji Scavenger Hunt mobile demo, which used the device camera to detect real-world objects matching emoji prompts. Both demos ran entirely in the browser, with no server-side ML.
After the launch, TensorFlow.js development tracked the broader TensorFlow release cadence and added several major capabilities:
@tensorflow/tfjs-node and @tensorflow/tfjs-node-gpu packages exposed native TensorFlow C++ kernels to JavaScript on servers, with optional CUDA acceleration on NVIDIA GPUs.@tensorflow/tfjs-react-native package brought GPU-accelerated TFJS to mobile apps via Expo's expo-gl and expo-gl-cpp.@tensorflow/tfjs umbrella package as of this writing, with TypeScript as the dominant source language for the project.In parallel, Google's broader on-device ML stack evolved. The team rebranded TensorFlow Lite as LiteRT in 2024 and introduced LiteRT.js, a Web AI inference path that targets browser deployment of LiteRT (.tflite) models. LiteRT.js can be used alongside TensorFlow.js or as an alternative for browsers that benefit from TFLite's quantisation tooling.
TensorFlow.js is structured as a small core library plus optional backend modules and higher-level APIs. The pieces fit together in three layers.
The Core API (@tensorflow/tfjs-core) provides low-level tensor operations, similar to NumPy with automatic differentiation added on top. Tensors are immutable and typed; ops include tf.matMul, tf.conv2d, tf.add, tf.softmax, and several hundred others. Eager execution is the default, meaning each op runs immediately when called. Automatic differentiation is provided through tf.grad and tf.variableGrads, allowing custom training loops and gradient-based optimisation outside the high-level API.
The Core API also exposes memory management primitives. Because WebGL textures live outside the JavaScript garbage collector, developers wrap operations in tf.tidy() to release intermediate tensors automatically, or call dispose() manually on long-lived tensors.
The Layers API (@tensorflow/tfjs-layers) is a Keras-compatible high-level API for building, training, and saving neural networks. It supports tf.sequential() for stacked-layer models and tf.model() for arbitrary directed graphs, plus model.compile, model.fit, model.evaluate, and model.predict matching Python Keras conventions. Layers include dense, convolutional, recurrent (LSTM, GRU), normalisation, dropout, and embedding variants. Models written with the Layers API can be saved as a model.json topology file plus binary weight shards, then loaded in any TFJS environment.
A backend is a TensorFlow.js plugin that implements the actual numeric kernels. The library can be initialised with a specific backend or it will pick the fastest available one. The table below summarises the main backends and where each is used.
| Backend | Runtime | Acceleration | Typical use |
|---|---|---|---|
| WebGL | Browser | GPU via WebGL 2.0 fragment shaders | Default browser backend; best for medium and large CNNs |
| WebGPU | Browser | GPU via WebGPU compute shaders | Newer browsers; targets best-of-class performance |
| WASM | Browser, Node.js | CPU via compiled XNNPACK kernels with SIMD and threads | Small or quantised models; fallback when GPU is unavailable |
| CPU (Plain JS) | Browser, Node.js | Pure JavaScript | Universal fallback; slow but always available |
| Node.js (CPU) | Node.js | Native TensorFlow C++ kernels | Server-side training and inference |
| Node.js (GPU) | Node.js | Native TensorFlow C++ + CUDA | Server-side training on NVIDIA GPUs |
| React Native | Mobile | GPU via expo-gl (WebGL) | iOS and Android apps |
The WebGL backend is the original GPU path inherited from deeplearn.js. Tensors are stored as WebGL textures and ops are implemented as fragment shaders that read input textures and write output textures. The TensorFlow.js team has reported the WebGL backend running up to roughly 100x faster than the plain JS CPU backend on common CNNs, with most of the time spent in texture fetches rather than floating-point math. Shader programs are compiled lazily on first use and cached, so warm-up cost is paid once per shape.
The WebGPU backend uses the newer WebGPU API, which exposes compute shaders and explicit GPU memory management instead of repurposing the graphics pipeline. The package @tensorflow/tfjs-backend-webgpu requires a browser with WebGPU enabled. Reports from the TensorFlow.js team indicate WebGPU can deliver around 3x faster inference than WebGL for typical ML workloads, with sub-30 ms response times on small and mid-sized models.
The WASM backend (@tensorflow/tfjs-backend-wasm) compiles Google's XNNPACK kernel library to WebAssembly. As of TensorFlow.js 2.3.0, the WASM backend gained SIMD vector instructions and multithreading support, making it roughly 10x faster than the plain JS backend on common models. WASM is on par with or faster than WebGL for small models such as MediaPipe BlazeFace and FaceMesh because it avoids the fixed overhead of compiling shaders. For medium and large CNNs like MobileNet and BodyPix, WebGL is generally 2x to 4x faster than WASM.
The CPU backend is a pure JavaScript implementation, included as a universal fallback when neither WebGL, WebGPU, nor WASM is available. It is slow but works in any JavaScript host.
The Node.js backends bind directly to TensorFlow's C++ runtime through Node native add-ons. The CPU package is @tensorflow/tfjs-node; the GPU package is @tensorflow/tfjs-node-gpu and requires a CUDA-capable NVIDIA GPU and the matching CUDA toolkit. On Node, performance is comparable to Python TensorFlow because both frontends drive the same C++ kernels.
TensorFlow.js supports full backpropagation in the browser, which is unusual for a JavaScript ML library. This enables several patterns that would otherwise require a backend server:
The Layers API exposes the standard model.fit workflow, and the Core API exposes tf.grad and tf.variableGrads for custom training loops.
Most real applications load a pretrained model and run inference. TensorFlow.js loads models from a model.json file (containing the topology and weight manifest) plus one or more .bin shards (containing the weights). The tf.loadGraphModel function loads converted TensorFlow SavedModels, and tf.loadLayersModel loads converted Keras models or models saved from the TFJS Layers API.
The tfjs-converter is a Python command-line tool, installed via pip install tensorflowjs, that converts existing models to the TensorFlow.js web format. Supported source formats include:
| Source format | TFJS loader |
|---|---|
| TensorFlow SavedModel | tf.loadGraphModel |
Keras HDF5 (.h5) | tf.loadLayersModel |
| TensorFlow Hub modules | tf.loadGraphModel |
TFLite (.tflite), via TensorFlow Lite (LiteRT) tooling and tf2onnx | varies |
| PyTorch (via ONNX export plus tf2onnx) | varies |
The converter writes a directory containing model.json and sharded weight files, with shards typically capped at 4 MB each so they cache well in browsers.
Google maintains a separate repository, tensorflow/tfjs-models, of pre-trained models published as npm packages under the @tensorflow-models/* scope. The most commonly used models are listed below.
| Model | Task | Notes |
|---|---|---|
| MoveNet | Single-person pose estimation | 17 keypoints; runs at 50+ fps on modern phones; Lightning and Thunder variants |
| PoseNet | Multi-person pose estimation | Older single- and multi-pose model; superseded by MoveNet for single-pose use |
| BodyPix | Person and body-part segmentation | 24-part segmentation; used for backgrounds and AR effects |
| HandPose | Hand landmark detection | 21 keypoints per hand; predicts handedness |
| Face Landmarks Detection | Face mesh and attention | Builds on MediaPipe Face Mesh |
| Face Detection | Face bounding-box detection | Lightweight detector for camera streams |
| MobileNet | Image classification | ImageNet labels; small enough to fit in a few megabytes |
| COCO-SSD | Object detection | Single Shot MultiBox Detector trained on COCO |
| Universal Sentence Encoder | Text embeddings | 512-dimensional sentence embeddings for similarity and classification |
| Speech Commands | Keyword spotting | Recognises a small set of voice commands from microphone audio |
| KNN Classifier | Nearest-neighbour classifier | Used as a head for transfer learning on top of MobileNet features |
| Toxicity | Toxic comment classification | Multi-label classifier for offensive language |
In addition, Magenta.js (@magenta/music) is a JavaScript port of Google's Magenta research library for music and art generation. It uses TensorFlow.js for GPU-accelerated inference and ships browser-compatible implementations of MusicVAE (variational autoencoder for melodies, drums, and trios), MusicRNN (LSTM-based MelodyRNN, DrumsRNN, ImprovRNN, PerformanceRNN), and GANSynth (GAN-based audio synthesis). Most Magenta.js models can run inside a Web Worker; GANSynth and Onsets and Frames need direct access to the page's AudioContext to synthesise audio.
TensorFlow.js is used for tasks that benefit from running models close to the user. The most common categories are:
The practical performance of TensorFlow.js depends heavily on the chosen backend, the model architecture, and the device. Some rough numbers, drawn from the TensorFlow.js team's own benchmarks and blog posts:
tfjs-node and tfjs-node-gpu call the same C++ kernels as Python TensorFlow, performance is comparable to native Python TensorFlow on the same hardware.Memory bandwidth is often the bottleneck for the WebGL backend rather than floating-point throughput, because each op fetches input textures and writes output textures rather than fusing kernels the way a native CUDA implementation would.
A number of JavaScript libraries target client-side ML, with different trade-offs in vendor support, model formats, and focus.
| Library | Vendor | Year | Backends | Model formats | Focus |
|---|---|---|---|---|---|
| TensorFlow.js | 2018 | WebGL, WebGPU, WASM, CPU, Node native | TFJS, SavedModel via converter, Keras .h5 | General-purpose ML in browser, Node, and React Native | |
| ONNX Runtime Web | Microsoft | 2021 | WebGL, WebGPU, WebNN, WASM | ONNX | Cross-framework inference; pairs with PyTorch via ONNX export |
| Transformers.js | Hugging Face | 2023 | Built on ONNX Runtime Web (WebGPU, WASM) | ONNX-converted Hugging Face models | NLP and vision pipelines mirroring Python transformers |
| ml5.js | NYU ITP / community | 2018 | Built on TensorFlow.js | TFJS-compatible models | Friendly API for artists and educators |
| brain.js | Community | 2015 | CPU, WebGL via gpu.js | Internal | Simple feed-forward and recurrent networks |
| ConvNetJS | Andrej Karpathy | 2014 | CPU only | Internal | Educational; pre-deeplearn.js era |
| Synaptic | Community | 2014 | CPU only | Internal | Architecture-free recurrent networks |
| LiteRT.js | 2024 | WebGPU, WASM | LiteRT (.tflite) | Browser path for the TensorFlow Lite (LiteRT) ecosystem |
Transformers.js and ONNX Runtime Web have taken much of the LLM and transformer-inference traffic that might otherwise have gone to TensorFlow.js, in part because most large open-weight models are released first as PyTorch checkpoints and exported to ONNX rather than to the TFJS native format. TensorFlow.js remains the more natural choice for projects that already live in the TensorFlow or Keras ecosystem, for in-browser training, and for the rich tfjs-models pretrained library.
teachablemachine.withgoogle.com): Google's no-code in-browser tool for training image, sound, and pose classifiers. The current version supports three input types (images and videos, sounds, body positions). All training happens locally; the tool exports models for use in TensorFlow.js, TensorFlow Lite, or full TensorFlow.TensorFlow.js is constrained by the browser sandbox more than by anything intrinsic to its design.
.tflite models that complements TensorFlow.js for browser deployment.tensorflow/tfjs repository has been steadily maintained, with version 4.22.0 released in October 2024 and ongoing kernel and converter improvements.TensorFlow.js is framework-agnostic and works with React, Vue, Angular, Svelte, and vanilla JavaScript. Two installation paths are common:
npm install @tensorflow/tfjs adds the umbrella package, which includes the Core API, Layers API, default browser backends (WebGL, CPU), and the Data and Vis utilities. Specific backends can be installed separately, for example @tensorflow/tfjs-backend-webgpu or @tensorflow/tfjs-backend-wasm.<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> from a CDN gives a window.tf global without a build step, which is convenient for prototyping.The library is written in TypeScript and ships its own type definitions, so editors can autocomplete tensor shapes and op names. Tree-shaking is supported when importing only the kernels and ops a project uses.
TensorFlow.js is released under the Apache 2.0 license, the same license as the rest of the TensorFlow ecosystem. The project is governed by Google with significant external contributions; the source lives at github.com/tensorflow/tfjs. Pre-trained models live in the parallel tensorflow/tfjs-models repository, also under Apache 2.0.