What is wasm-feature-detect?
The wasm-feature-detect npm package is designed to help developers detect the availability of various WebAssembly (Wasm) features in the user's environment. This can be particularly useful for ensuring compatibility and optimizing performance by leveraging specific Wasm capabilities when they are available.
What are wasm-feature-detect's main functionalities?
Detect SIMD Support
This feature allows you to detect if SIMD (Single Instruction, Multiple Data) is supported in the user's environment. SIMD can significantly improve performance for certain types of computations.
const wasmFeatureDetect = require('wasm-feature-detect');
wasmFeatureDetect.simd().then(supported => {
if (supported) {
console.log('SIMD is supported');
} else {
console.log('SIMD is not supported');
}
});
Detect Bulk Memory Operations Support
This feature checks if bulk memory operations are supported. Bulk memory operations can be used to efficiently manage memory in WebAssembly.
const wasmFeatureDetect = require('wasm-feature-detect');
wasmFeatureDetect.bulkMemory().then(supported => {
if (supported) {
console.log('Bulk Memory Operations are supported');
} else {
console.log('Bulk Memory Operations are not supported');
}
});
Detect Multi-Value Support
This feature detects if the multi-value extension is supported. Multi-value allows WebAssembly functions to return multiple values, which can simplify certain programming patterns.
const wasmFeatureDetect = require('wasm-feature-detect');
wasmFeatureDetect.multiValue().then(supported => {
if (supported) {
console.log('Multi-Value is supported');
} else {
console.log('Multi-Value is not supported');
}
});
Other packages similar to wasm-feature-detect
wasm-check
The wasm-check package is another alternative for detecting WebAssembly features. It provides a simple API to check for the availability of specific Wasm capabilities. While it covers many of the same features as wasm-feature-detect, it may have a different set of supported features and a different API design.
WebAssembly Feature Detection
A small library to detect which features of WebAssembly are supported.
- ✅ Runs in all major browsers
- ✅ Runs in Node
- ✅ Provided as an ES6 module, CommonJS and UMD module.
- ✅ CSP compatible
- ✅ Only ~550B gzipped
Installation
npm install -g wasm-feature-detect
Usage
<script type="module">
import { simd } from "https://unpkg.com/wasm-feature-detect?module";
simd().then(simdSupported => {
if (simdSupported) {
} else {
}
});
</script>
If required, there’s also a UMD version
<script src="https://unpkg.com/wasm-feature-detect/dist/umd/index.js"></script>
<script>
wasmFeatureDetect.simd().then();
</script>
Detectors
All detectors return a Promise<bool>
.
Why are all the tests async?
The technical reason is that some tests might have to be augmented to be asynchronous in the future. For example, Firefox is planning to make a change that would require a postMessage
call to detect SABs, which are required for threads.
The other reason is that you should be using WebAssembly.compile
, WebAssembly.instantiate
, or their streaming versions WebAssembly.compileStreaming
and WebAssembly.instantiateStreaming
, which are all asynchronous. You should already be prepared for asynchronous code when using WebAssembly!
Contributing
If you want to contribute a new feature test, all you need to do is create a new folder in src/detectors
and it will be automatically picked up. The folder must contain a module.wat
file, which will be compiled using wabt.js
.
;; Name: <Name of the feature for the README>
;; Proposal: <Link to the proposal’s explainer/repo>
;; Features: <Space-separated list of WasmFeatures from wabt.js>
(module
;; More WAT code here
)
The folder can also contain an optional index.js
file, whose default export must be an async function. This function can do additional testing in JavaScript and must return a boolean. See the “threads” detector as an example.
License Apache-2.0