Lodash rangeRight Negative Step Sequences Explained

Lodash's _.rangeRight method produces descending or inverted sequence intervals by utilizing the exact boundary calculations of the standard _.range method while reversing the population order of the resulting array. When handling negative steps, the method computes the total sequence length upfront to prevent runaway loops, precisely mapping the progression from the logical termination point back to the start. This article examines how Lodash natively implements this mechanism, calculates negative offsets, and prevents off-by-one errors during inverted interval creation.

The Underlying Mechanics: createRange and baseRange

In the Lodash source code, _.rangeRight is not a distinct algorithm from _.range. Instead, both methods are created via an internal higher-order function named createRange:

var rangeRight = createRange(true);

Passing true sets an internal boolean flag (fromRight). While the primary sequence bounds—start, end, and step—are calculated identically for both functions, fromRight dictates whether values populate the allocated array starting from index 0 upward or from the final index downward.

Step Direction and Length Determination

Before generating values, Lodash normalizes inputs to ensure the step matches the direction of the range. If a negative step is provided, the interval must progress downward (where start > end).

To avoid the performance penalties and infinite loop risks associated with standard while or for loops driven by floating-point step evaluations, Lodash precomputes the exact length of the required array natively using standard arithmetic:

length = Math.max(Math.ceil((end - start) / (step || 1)), 0);

If the combination of start, end, and a negative step results in an invalid progression (such as start < end with a negative step), (end - start) / step results in a negative number, resolving the length to 0 and returning an empty array cleanly.

Processing Negative Increments

When step is negative and properly directed, Lodash produces an inverted output sequence by altering write indices rather than flipping the sign of iteration:

  1. Standard _.range Execution: Given _.range(0, -4, -1):

    • Array length: Math.ceil((-4 - 0) / -1) = 4
    • Increments: 0, -1, -2, -3
    • Result: [0, -1, -2, -3]
  2. Inverted _.rangeRight Execution: Given _.rangeRight(0, -4, -1):

    • Array length: 4
    • Generation starts identically: current = start (0)
    • Instead of assigning values at index++, Lodash writes values from length - 1 down to 0:
      • Index 3: 0
      • Index 2: -1
      • Index 1: -2
      • Index 0: -3
    • Result: [-3, -2, -1, 0]

Native Memory Allocation

Because the array length is calculated prior to insertion, Lodash allocates memory for the full array in a single operation (new Array(length)). Writing directly into pre-allocated indices via result[fromRight ? --length : ++index] = current ensures:

By separating the boundary calculation from the write order, _.rangeRight ensures that any valid interval with a deeply negative step terminates strictly at the intended boundary and renders in clean, reverse-chronological order.