 (defn- vector-index-out-of-bounds [i cnt]
   (throw (js/Error. (str "No item " i " in vector of length " cnt))))
 
-(defn- array-for [pv i]
-  (if (and (<= 0 i) (< i (.-cnt pv)))
-    (if (>= i (tail-off pv))
+(defn- first-array-for-longvec [pv]
+  ;; invariants: (count pv) > 32.
+  (loop [node (.-root pv)
+         level (.-shift pv)]
+    (if (pos? level)
+      (recur (pv-aget node 0) (- level 5))
+      (.-arr node))))
+
+(defn- unchecked-array-for [pv i]
+  ;; invariant: i is a valid index of pv (use array-for if unknown).
+  (if (>= i (tail-off pv))
       (.-tail pv)
       (loop [node (.-root pv)
              level (.-shift pv)]
         (if (pos? level)
           (recur (pv-aget node (bit-and (bit-shift-right-zero-fill i level) 0x01f))
                  (- level 5))
-          (.-arr node))))
+          (.-arr node)))))
+
+(defn- array-for [pv i]
+  (if (and (<= 0 i) (< i (.-cnt pv)))
+    (unchecked-array-for pv i)
     (vector-index-out-of-bounds i (.-cnt pv))))
 
 (defn- do-assoc [pv level node i val]

     (== 1 cnt) (-with-meta cljs.core.PersistentVector.EMPTY meta)
     (< 1 (- cnt (tail-off coll)))
      (PersistentVector. meta (dec cnt) shift root (.slice tail 0 -1) nil)
      :else (let [new-tail (array-for coll (- cnt 2))
                  nr (pop-tail coll shift root)
                  new-root (if (nil? nr) cljs.core.PersistentVector.EMPTY_NODE nr)
                  cnt-1 (dec cnt)]

    (aget (array-for coll n) (bit-and n 0x01f)))
  (-nth [coll n not-found]
    (if (and (<= 0 n) (< n cnt))
      (aget (unchecked-array-for coll n) (bit-and n 0x01f))
      not-found))

  ILookup
