Lodash _.sortedIndexOf() method
What you’ll learn
- How
_.sortedIndexOf(array, value)spots the first slot whose element equalsvalueunder SameValueZero. - Why missing needles yield
-1, unlike _.sortedIndex() insertion semantics. - When logarithmic search beats _.indexOf() on large sorted buffers.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.sortedIndex() for bisect-left inserts and _.indexOf() for linear scans on unsorted data.
- You accept that the collection must already be sorted ascending for default comparisons.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.sortedIndexOf wraps the familiar index lookup question—“do we already store this exact value?”—with the performance profile of binary search when ordering guarantees hold.
Logarithmic seek
Each probe halves the candidate span instead of scanning every element like linear indexOf.
First hit
Duplicate runs resolve to the earliest matching index so callers dedupe predictably.
Honest misses
-1 signals absence outright—no accidental insertion slot disguised as success.
Syntax
_.sortedIndexOf(array, value) - array: ascending sorted collection scanned via binary search.
- value: needle compared with SameValueZero semantics.
- Returns: lowest matching index, or
-1when nothing compares equal.
Value present
A clean hit returns the index where the element lives.
import sortedIndexOf from "lodash/sortedIndexOf";
const ids = [10, 20, 30, 40];
sortedIndexOf(ids, 30);
// → 2 Value absent
When nothing equals the needle, Lodash mirrors indexOf with -1 instead of proposing an insertion slot.
import sortedIndexOf from "lodash/sortedIndexOf";
sortedIndexOf([1, 3, 5, 7], 4);
// → -1 Duplicate runs
Equal neighbors collapse to the first matching index—pair with _.sortedLastIndexOf() when you need the upper fence.
import sortedIndexOf from "lodash/sortedIndexOf";
const tiers = [10, 20, 20, 20, 30];
sortedIndexOf(tiers, 20);
// → 1 📋 _.sortedIndexOf vs _.sortedIndex vs _.indexOf
| API | Outcome when needle missing | Notes |
|---|---|---|
_.sortedIndexOf(array, value) | -1 | Binary search hit test with SameValueZero. |
_.sortedIndex(array, value) | Insertion index in [0, length] | Always returns where to splice, even if value absent. |
_.indexOf(array, value) | -1 | Linear scan; sorting not required. |
_.sortedLastIndexOf(array, value) | -1 | Last equal index on sorted data (binary search). |
Pitfalls to avoid
Broken ordering
Descending sorts, unstable merges, or partially sorted buffers invalidate every midpoint decision.
Reference equality
Objects match only by reference unless you compare primitives—reach for projections via other helpers when rows are structs.
Needing the upper bound
Counts spanning duplicate bands may require _.sortedLastIndexOf() minus sortedIndexOf.
❓ FAQ
Summary
- Purpose:
_.sortedIndexOf(array, value)finds the lowest index whose element equalsvalue, or-1. - Requirement: ascending sorted inputs for correct binary search.
- Next: Lodash _.sortedLastIndex(), _.sortedIndexBy() (previous), or the array methods hub.
_.sortedIndex() answers where to insert; sortedIndexOf answers whether the slot already exists—think “bisect confirm” rather than “bisect allocate.”
6 people found this page helpful
