Subset Sum 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
- ✓Subset Sum runs in O(n*S) time and O(n*S) space, in JavaScript as in every other language.
- ✓There is no bit-set type. The realistic row is new Uint8Array(target + 1), which is dense, one byte per cell and already zero-initialised, so no fill() is needed.
- ✓Reviewers want Uint8Array over new Array(n).fill(false), the descending inner loop for (let s = target; s >= x; s--), and an early return once reachable[target] is set.
- ✓The JavaScript trap: JavaScript's bitwise operators coerce both sides to signed 32-bit integers, so the tempting bits |= bits << x on a Number is broken the moment a sum passes 31: 1 << 31 is negative and 1 << 32 is 1, not 2^32.
- ✓Uint8Array reads are monomorphic and JIT down to a single load plus an implicit bounds check.
What Subset Sum actually is
Subset Sum in JavaScript: what you actually reach for
JavaScript implementation
function subsetSum(nums, target) {
if (!Number.isInteger(target) || target < 0) return false;
const reachable = new Uint8Array(target + 1);
reachable[0] = 1;
for (const x of nums) {
if (x <= 0 || x > target) continue;
for (let s = target; s >= x; s--) {
if (reachable[s - x]) reachable[s] = 1;
}
if (reachable[target]) return true;
}
return reachable[target] === 1;
}
function subsetSumBigInt(nums, target) {
let bits = 1n;
const mask = (1n << BigInt(target + 1)) - 1n;
for (const x of nums) {
if (x <= 0 || x > target) continue;
bits = (bits | (bits << BigInt(x))) & mask;
}
return ((bits >> BigInt(target)) & 1n) === 1n;
}
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(n*S)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Subset Sum?
There is no bit-set type. The realistic row is new Uint8Array(target + 1), which is dense, one byte per cell and already zero-initialised, so no fill() is needed. For the shift-or trick you need BigInt, which supports <<, | and & at arbitrary width: bits |= bits << BigInt(x). A Set of reachable sums only pays off when the reachable set stays sparse relative to S. Array.prototype.fill and Array.from exist but a plain boolean Array is the wrong container here for reasons the perf note covers.
What is the most common JavaScript mistake when implementing Subset Sum?
JavaScript's bitwise operators coerce both sides to signed 32-bit integers, so the tempting bits |= bits << x on a Number is broken the moment a sum passes 31: 1 << 31 is negative and 1 << 32 is 1, not 2^32. The shift-or trick only works on BigInt, and BigInt refuses to mix with Number, so bits << x throws 'Cannot mix BigInt and other types' unless you write BigInt(x). Separately, ordinary sums stop being exact above Number.MAX_SAFE_INTEGER (2^53 - 1), so a huge target can compare equal to a value it is not.
How does Subset Sum actually perform in JavaScript?
Uint8Array reads are monomorphic and JIT down to a single load plus an implicit bounds check. A plain Array of booleans starts in PACKED_ELEMENTS, and the first hole transitions it to HOLEY_ELEMENTS: still a fast elements kind, but every load now needs a hole check and a prototype-chain lookup on a miss, so it loses the tight code the packed kind gets. Only genuinely sparse arrays fall all the way back to dictionary (slow) elements, where the per-access cost is an order of magnitude worse. Either way new Uint8Array skips the whole elements-kind lattice. Tier-up matters: the first several thousand iterations run in the interpreter before the optimising compiler kicks in, so timing a small n measures warmup, not the algorithm. Each BigInt |= allocates a fresh heap value, trading GC churn for far fewer iterations. Time complexity is O(n*S) and space is O(n*S).
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 Replit 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.