Underscore.js _.before() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

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)
CallWhat 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

ParameterDescription
countRequired. Call number when invoking stops
functionRequired. Original function to wrap

Return Value

Return ValueDescription
*New wrapper function

Underscore.js Examples

Example 1 — Official: Ask Twice

Allow two real invocations; later calls reuse the second result.

Underscore
var asks = 0;
function askForRaise() {
  asks += 1;
  return "ask#" + asks;
}

var monthlyMeeting = _.before(3, askForRaise);

monthlyMeeting(); // → "ask#1"
monthlyMeeting(); // → "ask#2"
monthlyMeeting(); // → "ask#2"  (frozen)
monthlyMeeting(); // → "ask#2"
Try It Yourself

How It Works

  1. count = 3 → at most 2 real runs.
  2. Calls 1–2 invoke askForRaise.
  3. Call 3+ return the second result.
  4. Side effects stop after the second ask (asks stays 2).

Example 2 — Cap Side Effects

Limit how many times a log (or retry) may fire.

Underscore
var log = [];
var push = _.before(4, function (msg) {
  log.push(msg);
  return log.length;
});

push("a"); // 1
push("b"); // 2
push("c"); // 3
push("d"); // still 3 — "d" not pushed
// log → ["a", "b", "c"]
Try It Yourself

How It Works

  1. before(4) allows 3 real pushes.
  2. Fourth call skips fn and returns 3.
  3. "d" never lands in log.
  4. Handy for capping retries or UI actions.

Example 3 — _.before(2) ≈ _.once

One real run, then reuse that result.

Underscore
var n = 0;
var init = _.before(2, function () {
  n += 1;
  return "app#" + n;
});

init(); // → "app#1"
init(); // → "app#1"
init(); // → "app#1"
// n → 1
Try It Yourself

How It Works

  1. before(2) → at most 1 real run.
  2. Same idea as _.once.
  3. Prefer _.once when the intent is “run once” — clearer name.
  4. Use before(n) when you need more than one allowed run.

Example 4 — _.before vs _.after

Opposite timing: stop early vs start late.

Underscore
var beforeLog = [];
var afterLog = [];

var b = _.before(3, function (n) { beforeLog.push(n); return n; });
var a = _.after(3, function (n) { afterLog.push(n); });

b(1); b(2); b(3); b(4);
// beforeLog → [1, 2]

a(1); a(2); a(3); a(4);
// afterLog → [3, 4]
Try It Yourself

How It Works

  1. before(3) runs on calls 1–2, freezes after.
  2. after(3) ignores 1–2, runs on 3+.
  3. Mirror tools for “cap early” vs “wait then go”.
  4. Pick by intent — limit action vs wait for N signals.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.before Underscore 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.

Next: _.wrap()

Wrap a function inside another to run code before/after it.

_.wrap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful