Fast Fourier Transform Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Fast Fourier Transform runs in O(n log n) time and O(n) space, in JavaScript as in every other language.
- ✓There is no FFT in the language and none in Node. In the browser the platform ships one only inside Web Audio: AnalyserNode, from audioCtx.createAnalyser(), with fftSize (a power of two from 32 to 32768), frequencyBinCount === fftSize/2, and getFloatFrequencyData(Float32Array) / getByteFrequencyData(Uint8Array).
- ✓Two Float64Arrays (re, im), or one interleaved buffer - never an array of {re, im} objects; that gets rejected on sight.
- ✓The JavaScript trap: In the browser, AnalyserNode is not a raw FFT and code ships assuming it is. It applies a Blackman window, converts to decibels, and then blends each frame with the previous one using smoothingTimeConstant, whose default is 0.8 - so your 'spectrum' is an exponential moving average of the last few frames.
- ✓The optimising compiler keeps the butterfly fast only while the arrays stay monomorphic Float64Array; one plain Array or one hole deoptimises the whole loop.
What Fast Fourier Transform actually is
Fast Fourier Transform in JavaScript: what you actually reach for
JavaScript implementation
// in-place radix-2; n a power of two, split real/imag Float64Arrays
function fft(re, im) {
const n = re.length;
for (let i = 1, j = 0; i < n; i++) { // bit-reversal permutation
let bit = n >> 1;
for (; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if (i < j) {
let t = re[i]; re[i] = re[j]; re[j] = t;
t = im[i]; im[i] = im[j]; im[j] = t;
}
}
for (let len = 2; len <= n; len <<= 1) {
const half = len >> 1, ang = -2 * Math.PI / len;
const wr = Math.cos(ang), wi = Math.sin(ang);
for (let i = 0; i < n; i += len) {
let cr = 1, ci = 0;
for (let k = 0; k < half; k++) {
const a = i + k, b = a + half;
const vr = re[b] * cr - im[b] * ci;
const vi = re[b] * ci + im[b] * cr;
re[b] = re[a] - vr; im[b] = im[a] - vi;
re[a] += vr; im[a] += vi;
const nr = cr * wr - ci * wi;
ci = cr * wi + ci * wr; cr = nr;
}
}
}
}
const re = Float64Array.from({ length: 8 }, (_, i) => i);
fft(re, new Float64Array(8));
console.log(re[0]); // 28 = sum of 0..7Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Fast Fourier Transform?
There is no FFT in the language and none in Node. In the browser the platform ships one only inside Web Audio: AnalyserNode, from audioCtx.createAnalyser(), with fftSize (a power of two from 32 to 32768), frequencyBinCount === fftSize/2, and getFloatFrequencyData(Float32Array) / getByteFrequencyData(Uint8Array). For arbitrary data the de-facto package is fft.js, an iterative radix-4 transform over interleaved complex arrays; it won on being dependency-free and small, and it works fine over a Float64Array you allocate yourself - its own createComplexArray() hands back a plain Array, so allocate the buffer on your side if you want the monomorphic typed-array fast path.
What is the most common JavaScript mistake when implementing Fast Fourier Transform?
In the browser, AnalyserNode is not a raw FFT and code ships assuming it is. It applies a Blackman window, converts to decibels, and then blends each frame with the previous one using smoothingTimeConstant, whose default is 0.8 - so your 'spectrum' is an exponential moving average of the last few frames. getByteFrequencyData additionally clamps and rescales into minDecibels..maxDecibels. And frequencyBinCount is fftSize/2, so sizing your array with fftSize leaves half of it untouched. Set smoothingTimeConstant = 0 before measuring anything.
How does Fast Fourier Transform actually perform in JavaScript?
The optimising compiler keeps the butterfly fast only while the arrays stay monomorphic Float64Array; one plain Array or one hole deoptimises the whole loop. Bit-reversal written with <<, >> and & runs on int32, which is fine and fast at any realistic n. The dominant cost is garbage: allocating a fresh array per stage turns a one-millisecond transform into a GC-bound one. Hoist len>>1 and the twiddle recurrence out of the inner loop; the engine will not do it for you through an aliased typed array. Time complexity is O(n log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Devin offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.