ないうちに means "before something happens, while it still hasn't": you act while there is still time. It follows the ない-form of a verb, and the change it names is usually one you don't want.
。
Let's go home before it gets dark.
。
Eat it before it gets cold.
。
I brought in the washing before it rained.
often goes with it, to stress that the change hasn't come yet.
。
Let's get into the restaurant before it gets crowded.
ないうちに or 前に
after the dictionary form is a plain "before", for something that will happen anyway. ないうちに adds a closing window: once the change comes, it will be too late. For an ordinary sequence of actions, only 前に fits.
。
Let me write it down before I forget.
。
I brush my teeth before I go to bed.
- × 寝ないうちに歯を磨きます → 寝る前に歯を磨きます
Before you knew it
With or , ないうちに describes something that happened without your noticing. The main clause is in the past.
。
Before I knew it, time had passed.
。
I fell asleep without realizing it.