Segment Tree 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
- ✓Segment Tree runs in O(log n) time and O(n) space, in JavaScript as in every other language.
- ✓An Array, or better a typed array such as Int32Array or Float64Array when the element type is known.
- ✓Use a typed array for the tree when the values are integers that fit, Int32Array gives you a genuine contiguous buffer instead of a boxed Array, and the sift loops become dramatically faster.
- ✓The JavaScript trap: Every JavaScript number is an IEEE-754 double, so integers stay exact only up to Number.MAX_SAFE_INTEGER, which is 2^53 - 1.
- ✓Int32Array and Float64Array are contiguous and unboxed, so a typed-array segment tree runs several times faster than one over a plain Array of numbers and uses a quarter of the memory.
What Segment Tree actually is
Segment Tree in JavaScript: what you actually reach for
JavaScript implementation
class SegmentTree {
#n;
#tree;
// Float64Array keeps values exact to 2^53; use BigInt64Array beyond that.
constructor(data) {
this.#n = data.length;
this.#tree = new Float64Array(2 * this.#n);
this.#tree.set(data, this.#n);
for (let i = this.#n - 1; i > 0; i--) {
this.#tree[i] = this.#tree[2 * i] + this.#tree[2 * i + 1];
}
}
update(index, value) {
let i = index + this.#n;
this.#tree[i] = value;
while (i > 1) {
i >>= 1;
this.#tree[i] = this.#tree[2 * i] + this.#tree[2 * i + 1];
}
}
// Sum of [left, right)
query(left, right) {
let total = 0;
let lo = left + this.#n;
let hi = right + this.#n;
while (lo < hi) {
if (lo & 1) total += this.#tree[lo++];
if (hi & 1) total += this.#tree[--hi];
lo >>= 1;
hi >>= 1;
}
return total; // exact only while the running total stays under 2^53
}
}Think 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(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Segment Tree?
An Array, or better a typed array such as Int32Array or Float64Array when the element type is known. There is no tree structure in the standard library. BigInt exists and is the correct escape hatch when sums can exceed the safe integer range.
What is the most common JavaScript mistake when implementing Segment Tree?
Every JavaScript number is an IEEE-754 double, so integers stay exact only up to Number.MAX_SAFE_INTEGER, which is 2^53 - 1. A sum segment tree over a few million values in the billions crosses that line, and beyond it addition silently rounds, the tree returns a wrong answer with no error, no overflow and no warning. Int32Array is worse in a different way: it wraps at 2^31. When totals can get large the answer is BigInt or Float64Array with an accepted precision bound.
How does Segment Tree actually perform in JavaScript?
Int32Array and Float64Array are contiguous and unboxed, so a typed-array segment tree runs several times faster than one over a plain Array of numbers and uses a quarter of the memory. BigInt is correct but heap-allocates per value and is roughly an order of magnitude slower, so reach for it only when the range genuinely demands it. Time complexity is O(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 trialBefore paying for GitHub Copilot, check its student and free-tier options 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.