Creates a wrapper of function that memoizes its return value. From the count-th call onwards, the memoized result of the last invocation is returned immediately instead of invoking function again — so the wrapper invokes function at most count - 1 times (official Underscore docs). Stable Function helper — not deprecated.
Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.
Syntax
Call _.before. Start with this line.
Underscore
_.before(count, function)
Call
What it does
_.before(count, function)
Primary signature.
_.before(...)
Call with the arguments your case needs.
Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.
Library declarations
Underscore
_.before(count, function)
Parameters
Parameter
Description
count
Required. Call number when invoking stops
function
Required. Original function to wrap
Return Value
Return Value
Description
*
New wrapper function
Underscore.js Examples
Example 1 — Official: Ask Twice
Allow two real invocations; later calls reuse the second result.
Pick by intent — limit action vs wait for N signals.
Compatibility
Browser & Runtime Support
_.before ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.
✓ Stable · Underscore 1.x
_.before()
Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.
100%With Underscore 1.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.beforeUnderscore 1.x+
Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).
Frequently Asked Questions
_.before(count, function) returns a wrapper that invokes function at most count−1 times. From the count-th call onward it returns the memoized last result. Official: _.before(3, askForRaise) runs twice; later calls reuse the second result.
They return the result of the last real invocation. The original function is not called again.
_.before stops early (runs first count−1 calls). _.after starts late (runs from the count-th call onward).
_.once(fn) is _.before(2, fn) — one real run, then reuse that result.
No. It only stores the last return value after the call limit — not a cache keyed by arguments like _.memoize.
Cap retries, limit how many times an action may run, or guard expensive setup without a manual counter.