Lodash _.sortedIndexOf() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.sortedIndexOf(array, value) spots the first slot whose element equals value under 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

javascript
_.sortedIndexOf(array, value)
  • array: ascending sorted collection scanned via binary search.
  • value: needle compared with SameValueZero semantics.
  • Returns: lowest matching index, or -1 when nothing compares equal.
1

Value present

A clean hit returns the index where the element lives.

javascript
import sortedIndexOf from "lodash/sortedIndexOf";

const ids = [10, 20, 30, 40];
sortedIndexOf(ids, 30);
// → 2
Try it Yourself
2

Value absent

When nothing equals the needle, Lodash mirrors indexOf with -1 instead of proposing an insertion slot.

javascript
import sortedIndexOf from "lodash/sortedIndexOf";

sortedIndexOf([1, 3, 5, 7], 4);
// → -1
Try it Yourself
3

Duplicate runs

Equal neighbors collapse to the first matching index—pair with _.sortedLastIndexOf() when you need the upper fence.

javascript
import sortedIndexOf from "lodash/sortedIndexOf";

const tiers = [10, 20, 20, 20, 30];
sortedIndexOf(tiers, 20);
// → 1
Try it Yourself

📋 _.sortedIndexOf vs _.sortedIndex vs _.indexOf

APIOutcome when needle missingNotes
_.sortedIndexOf(array, value)-1Binary 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)-1Linear scan; sorting not required.
_.sortedLastIndexOf(array, value)-1Last equal index on sorted data (binary search).

Pitfalls to avoid

Sort

Broken ordering

Descending sorts, unstable merges, or partially sorted buffers invalidate every midpoint decision.

Obj

Reference equality

Objects match only by reference unless you compare primitives—reach for projections via other helpers when rows are structs.

Dup

Needing the upper bound

Counts spanning duplicate bands may require _.sortedLastIndexOf() minus sortedIndexOf.

❓ FAQ

No. It is a read-only lookup that leaves ordering untouched.
Lodash returns -1, mirroring _.indexOf semantics rather than insertion indexes.
SameValueZero like indexOf-style searches: NaN matches NaN, and +0 matches -0 when deciding equality.
Yes—ascending default ordering is assumed; otherwise binary search walks off a cliff and answers are meaningless.
Use import sortedIndexOf from "lodash/sortedIndexOf" or require("lodash/sortedIndexOf") for tree-shaking friendly bundles.

Summary

Did you know?

_.sortedIndex() answers where to insert; sortedIndexOf answers whether the slot already exists—think “bisect confirm” rather than “bisect allocate.”

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.

6 people found this page helpful