gitmyhub

gpu.js

JavaScript ★ 15k updated 1y ago

GPU Accelerated JavaScript

A JavaScript library that automatically runs heavy number-crunching code on the GPU for big speed gains, working in browsers and Node.js with a transparent CPU fallback when no GPU is available.

JavaScriptTypeScriptsetup: easycomplexity 2/5

GPU.js is a JavaScript library that lets you run computations on a computer's GPU (graphics processing unit) from JavaScript code, either in a web browser or in Node (a runtime that lets JavaScript run outside the browser). A GPU is the chip normally used to draw graphics, but it is also very fast at doing many small numerical calculations in parallel — a technique called GPGPU, or general-purpose computing on GPUs.

The way it works is that you write a regular JavaScript function that describes how to compute one element of a result, for example one cell of a large matrix. GPU.js then automatically transpiles that function into shader language — the specialized code GPUs understand — and compiles it so it can run on the GPU. Many copies of your function then execute in parallel, which can make a heavy computation finish far faster than ordinary JavaScript. The README's headline example performs matrix multiplication on two 512-by-512 matrices, and notes that depending on hardware GPU.js typically runs 1 to 15 times faster than plain JavaScript. If no GPU is available, the same function will still run on the CPU as normal JavaScript.

You would reach for GPU.js when a JavaScript project needs to do a lot of math — image processing, simulations, fractals, cellular automata, graph algorithms, or scientific visualizations — and plain JavaScript is too slow. The README links to demos including Mandelbrot rendering, image and video convolution, heatmaps, Dijkstra's algorithm, Conway's Game of Life, and Voronoi diagrams.

The library is JavaScript with TypeScript typings, installs through npm or yarn, and can also be loaded into a browser via a CDN script tag.

Where it fits