What is detect-gpu?
The detect-gpu npm package is designed to detect the GPU (Graphics Processing Unit) of the user's device. It provides information about the GPU model, vendor, and performance tier, which can be useful for optimizing graphics-intensive applications.
What are detect-gpu's main functionalities?
Detect GPU Information
This feature allows you to detect the GPU information of the user's device. The `getGPUTier` function returns an object containing the GPU model, vendor, and performance tier.
const { getGPUTier } = require('detect-gpu');
(async () => {
const gpuTier = await getGPUTier();
console.log(gpuTier);
})();
Asynchronous GPU Detection
This feature allows you to perform GPU detection asynchronously with custom tier settings for mobile and desktop devices. The `getGPUTier` function can be customized to return different performance tiers based on the device type.
const { getGPUTier } = require('detect-gpu');
(async () => {
const gpuTier = await getGPUTier({ mobileTiers: [0, 1, 2, 3], desktopTiers: [0, 1, 2, 3] });
console.log(gpuTier);
})();
Other packages similar to detect-gpu
gpu.js
gpu.js is a JavaScript library for GPU-accelerated computing in the browser. It allows you to run complex computations on the GPU, which can significantly speed up performance for certain tasks. Unlike detect-gpu, which focuses on detecting GPU information, gpu.js is designed for leveraging the GPU for computational tasks.
webgl-detect
webgl-detect is a package that helps in detecting WebGL capabilities of the user's browser. It provides information about the WebGL version, supported extensions, and renderer details. While detect-gpu focuses on GPU detection, webgl-detect is more about understanding the WebGL capabilities of the browser.
Detect GPU
Classifies GPUs based on their 3D rendering benchmark score allowing the developer to provide sensible default settings for graphically intensive applications. Think of it like a user-agent detection for the GPU but more powerful.
Demo
Live demo
Installation
By default we use the UNPKG CDN to host the benchmark data. If you would like to serve the benchmark data yourself download the required benchmarking data from benchmarks.tar.gz and serve it from a public directory.
Make sure you have Node.js installed.
$ npm install detect-gpu
Usage
import { getGPUTier } from 'detect-gpu';
(async () => {
const gpuTier = await getGPUTier();
})();
detect-gpu
uses rendering benchmark scores (framerate, normalized by resolution) in order to determine what tier should be assigned to the user's GPU. If no WebGLContext
can be created, the GPU is blocklisted or the GPU has reported to render on less than 15 fps
tier: 0
is assigned. One should provide a fallback to a non-WebGL experience.
Based on the reported fps
the GPU is then classified into either tier: 1 (>= 15 fps)
, tier: 2 (>= 30 fps)
or tier: 3 (>= 60 fps)
. The higher the tier the more graphically intensive workload you can offer to the user.
API
getGPUTier({
benchmarksURL?: string;
glContext?: WebGLRenderingContext | WebGL2RenderingContext;
failIfMajorPerformanceCaveat?: boolean;
mobileTiers?: number[];
desktopTiers?: number[];
override?: {
renderer?: string;
isIpad?: boolean;
isMobile?: boolean;
screenSize?: { width: number; height: number };
loadBenchmarks?: (file: string) => Promise<ModelEntry[]>;
};
})
Support
Special care has been taken to make sure all browsers that support WebGL
are also supported by detect-gpu
including IE 11
.
Changelog
Changelog
Licence
My work is released under the MIT license.
detect-gpu
uses both mobile and desktop benchmarking scores from https://gfxbench.com.