polyfit uses recursive least squares to perform a polynomial regression (i.e. it fits a polynomial to a set of data points without requiring a large buffer). For details on the math behind this recursive least squares implementation, see Gentlemen & Kung's famous 1981 paper entitled "Matrix triangularization by systolic arrays".