Abstract
MILJS is a collection of state-of-the-art, platform-independent, scalable, fast JavaScript libraries for matrix calculation and machine learning. Our core library offering a matrix calculation is called Sushi, which exhibits far better performance than any other leading machine learning libraries written in JavaScript. Especially, our matrix multiplication is 177 times faster than the fastest JavaScript benchmark. Based on Sushi, a machine learning library called Tempura is provided, which supports various algorithms widely used in machine learning research. We also provide Soba as a visualization library. The implementations of our libraries are clearly written, properly documented and thus can are easy to get started with, as long as there is a web browser. These libraries are available from http://mil-tokyo.github.io/ under the MIT license.
Introduction
While web technologies have advanced rapidly, scientific computing and machine learning have largely remained tied to native applications. The primary bottleneck is JavaScript's single-threaded, event-driven nature, which traditionally makes parallelization—a strict requirement for computationally heavy matrix operations—difficult to achieve. While some JavaScript machine learning libraries exist, they are typically too slow for practical research use. MILJS tackles these speed limitations to enable fast, scalable, and platform-independent machine learning directly in the browser.
The MILJS Ecosystem
The authors developed a three-part suite called MILJS to provide an end-to-end data science ecosystem in JavaScript:
- Sushi (Matrix Calculation): The core engine. It bypasses the single-thread limit by utilizing WebCL (a JavaScript binding for OpenCL) to execute parallelized matrix calculations on multicore CPUs or GPUs. Sushi automatically detects the best available hardware platform, manages the OpenCL context, and executes kernels. If WebCL is unavailable, it falls back to a highly optimized native JavaScript implementation.
- Tempura (Machine Learning): Built on top of Sushi, this library implements standard ML algorithms including Linear Models (Lasso, Ridge, Logistic Regression, SGD-SVM), K-means clustering, Gaussian Mixture Models, and Principal Component Analysis (PCA).
- Soba (Visualization): A 2D plotting library integrated with Sushi to render scatter plots, line plots, and contour plots directly in the browser, aiding in the visual analysis of ML models.
Performance Benchmarks
The authors benchmarked Sushi against popular JS math libraries (Sylvester, math.js, and Google's Closure Library) across four tasks: matrix additions, various matrix multiplications, and a sequence mimicking a Perceptron's forward/backward pass.
- Massive Speedups: In a heavy matrix multiplication task (a 1000x100 matrix multiplied by a 100x1000 matrix) running on Node.js, Sushi with WebCL completed the operation in 16.4 ms, compared to Sylvester's 2910.0 ms—a staggering 177x speedup.
- Consistent Outperformance: Sushi with WebCL was the fastest across almost all tasks in both Firefox and Node.js environments. For example, adding two 1000x1000 matrices took just 2.0 ms in Node.js.
- Native JS Improvements: Even without WebCL, Sushi's native JavaScript implementation significantly outperformed math.js and Closure Library, and beat Sylvester in several tasks.
Key Insights and Implementation
By tapping into WebCL, JavaScript is no longer strictly bound by its single-threaded limitations. GPU acceleration in the browser is not just possible; it is highly effective for the matrix operations that underpin modern machine learning. However, memory management is critical; transferring data between the JavaScript runtime memory and OpenCL memory is a major performance bottleneck. Sushi optimizes this by keeping intermediate calculations on the GPU and only transferring the final required outputs back to the CPU.
Limitations and Future Work
The most significant limitation is that WebCL was not natively supported by major browsers at the time of publication, requiring specific extensions or custom browser forks. This limits immediate out-of-the-box adoption for general web users. Future work includes implementing more popular machine learning techniques, adding new convenience functions to the core libraries, and further optimizing algorithm efficiency.
Summary updated