Lodash _.isBuffer() method
What you’ll learn
- How
_.isBuffer(value)ties toBuffer.isBufferin Node. - Why
Uint8ArrayandArrayBufferare not Buffers. - Browser vs server considerations for binary APIs.
- When to normalize bytes with
Buffer.fromor typed arrays instead.
Prerequisites
Basic familiarity with Node.js buffers or comparable APIs from tooling tutorials.
- You know binary data appears as bytes, not UTF‑16 text strings.
- You can open Try-it labs: positive-buffer runs polyfill
Bufferand callBuffer.isBuffer(CDN lodash cannot bind_.isBufferto it).
Overview
Reach for _.isBuffer when validating streams, file reads, crypto digests, or middleware bodies delivered as Node Buffers—especially alongside lodash-heavy server utilities.
Node-first
Mirrors the runtime’s native Buffer branding checks.
Not typed arrays
Reject arbitrary Uint8Array unless your pipeline wraps them as Buffer.
Polyfill aware
Browsers need Buffer implementations—lodash cannot invent them.
Syntax
_.isBuffer(value) - value: any value to test.
- Returns:
trueifvalueis a Buffer (perBuffer.isBufferwhen available); otherwisefalse.
Buffer.from and Buffer.alloc
Typical constructors emit real Buffer instances, which lodash recognizes immediately.
import isBuffer from "lodash/isBuffer";
console.log(
"fromUtf8: " + isBuffer(Buffer.from("hello", "utf8")) + "\n" +
"allocBytes: " + isBuffer(Buffer.alloc(16))
); Typed arrays, ArrayBuffers, and strings
These values carry bytes or characters but are not Node Buffer instances—convert explicitly when your API requires Buffer.
import isBuffer from "lodash/isBuffer";
console.log(
"uint8View: " + isBuffer(new Uint8Array(4)) + "\n" +
"rawArrayBuffer: " + isBuffer(new ArrayBuffer(8)) + "\n" +
"plainString: " + isBuffer("binary-string")
); Wrapping typed arrays as Buffer
When interoperating with typed arrays, copy or wrap—then _.isBuffer succeeds.
import isBuffer from "lodash/isBuffer";
var view = new Uint8Array([1, 2, 3]);
console.log(
"typedOnly: " + isBuffer(view) + "\n" +
"wrappedCopy: " + isBuffer(Buffer.from(view))
); 📋 _.isBuffer vs related checks
| API | Matches |
|---|---|
_.isBuffer(x) | Node Buffer instances (via Buffer.isBuffer). |
_.isTypedArray(x) | Uint8Array, Float64Array, etc. |
_.isArrayBuffer(x) | Underlying raw ArrayBuffer objects. |
_.isArrayLike(x) | Broad collections—including Buffers when treated as indexed objects. |
Pitfalls to avoid
Multiple Buffer copies
Bundlers may shim Buffer differently across chunks—prefer one consistent polyfill per app.
arrayBuffer() vs Buffer
Fetch resolves ArrayBuffer; call Buffer.from on the server after reading bytes.
CDN lodash vs Buffer
Bare lodash.min.js cannot bind _.isBuffer to Buffer.isBuffer; polyfills arrive too late. Prefer bundlers with Node shims or call Buffer.isBuffer directly in scripts.
❓ FAQ
Summary
- Purpose: detect Node
Bufferinstances portably inside lodash. - Exclude: typed-array views and raw
ArrayBufferunless wrapped. - Next: explore more on Lodash _.isDate().
_.isBuffer compiles to Buffer.isBuffer only when Lodash can resolve Buffer via its CommonJS bootstrap path (Node and typical bundlers). The lodash.min.js CDN script used in bare browsers binds _.isBuffer to always-false; polyfilling Buffer later does not change that—call Buffer.isBuffer directly or bundle Lodash with Node-style globals.
6 people found this page helpful
