Determine if an object is a Buffer without including the full buffer module
npx @tessl/cli install tessl/npm-is-buffer@2.0.0is-buffer provides a lightweight utility function for determining whether an object is a Buffer instance without requiring the full buffer module in browserify environments. It offers cross-platform compatibility between Node.js and browser environments while maintaining zero runtime dependencies and minimal bundle size.
npm install is-bufferconst isBuffer = require('is-buffer');For ESM:
import isBuffer from 'is-buffer';For TypeScript:
import isBuffer = require('is-buffer');
// or modern syntax:
import isBuffer from 'is-buffer';const isBuffer = require('is-buffer');
// Check various types
console.log(isBuffer(Buffer.from('hello'))); // true
console.log(isBuffer(Buffer.alloc(4))); // true
console.log(isBuffer('string')); // false
console.log(isBuffer(null)); // false
console.log(isBuffer({})); // falseDetermines if an object is a Buffer instance by checking the object's constructor and its isBuffer method, providing reliable detection without including the full buffer module.
/**
* Determine if an object is a Buffer
* @param obj - The object to test
* @returns true if the object is a Buffer, false otherwise
*/
function isBuffer(obj: any): boolean;Parameters:
obj (any): The object to test for Buffer instanceReturns:
boolean: true if the object is a Buffer, false otherwiseImplementation Details:
false immediately if obj is null or undefinedobj.constructor exists and is not nullobj.constructor.isBuffer is a functionobj.constructor.isBuffer(obj) to perform the actual Buffer checkSupported Buffer Types:
Buffer.from() - Creates Buffer from various sourcesBuffer.alloc() - Creates zero-filled Buffer of specified sizeBuffer.allocUnsafeSlow() - Creates uninitialized BufferisBuffer methodNon-Buffer Types (returns false):
undefined, null, string, number, boolean{}, [], functionsisBuffer properties