# How to solve JavaScript heap out of memory on prime number?

As the ‘heap out of memory’ error message suggests, it occurs when any JavaScript code takes more memory than allocated memory, when we run any JavaScript program, the computer allocation particular memory to the JavaScript program.

When we run code of JavaScript or any programming language, our computer creates a process and allocates fixed memory. When a program needs more memory size, it raises an error like heap out of memory. For example, if we create an array of size 1020 and try to initialize every array index with some value, the heap goes out of memory and raises an error.

In this tutorial, we will learn to solve JavaScript heap out of memory while finding the prime factors of a very large number of values.

Users can follow the example below to visualize the heap out of memory error.

## Example (Visualizing the Error)

In the example below, we have created the getPrimeFactors() function, which returns the prime factors of any numeric value. It works perfectly when we pass a small numeric (near to 103), but when we pass a large numeric value (near 109) as a parameter to find prime factors, it gives an error, and the browser window becomes a black screen.

In this example, memory error occurs because we use two nested loops to iterate through the array, and the time complexity of the program becomes O(N2), which takes more memory than allocated memory.

<html>
<body>
<h2>Visualizing the <i>Process out of memory</i> while finding all prime factors of a number in JavaScript </h2>
</body>
<script>
try {
function getPrimeFactors(num) {
var factor = [];
let primes = [];
for (let m = 2; m <= num; m++) {
if (!factor[m]) {
primes.push(m);
for (let n = m << 1; n <= num; n += m) {
factor[n] = true;
}
}
}
return primes;
}
getPrimeFactors(1212121212323)
} catch (err) {
console.log(err.message)
}
</script>
</html>


In the output of the above example, users can observe the heap out of memory error. To get rid of it, we need to optimize the time and space complexity of the code.

Below, we will optimize the time complexity of the code written in example 1 to find all unique prime factors of the given number.

## Syntax

Users can follow the syntax below to write down optimized code to find unique prime factors of a given numeric value.

for (let m = 2; m * m <= value; m++) {
if (value % m === 0) {

// store factor to array
while (value % m === 0) {
value /= m;
}
}
}
if (value > 2) {

// store factor to array
}

In the above syntax, we make iterations until m*m is less than the value using the for loop. It means we make iterations until the square root of value is greater than m.

## Steps

Step 1 − Use the for loop to make iterations until the square root of the value is greater than m, where m is the initializer variable of the for loop.

Step 2 − In the for loop, if the value is divisible by m, it means m is a prime factor of the value and stores it in the array of factors.

Step 3 − After that, divide the value by m, and divide it multiple times if it is divisible multiple times by m using a while loop. Here, we need to store unique prime factors, so we store the value of m in the array only once.

Step 4 − Once all iterations of for loop are completed, check if the value is greater than 2. if yes, it means the value is the largest prime factor, and store it in the array.

## Example (Solving the Error)

The example below used the array to store the prime factors. Also, we have implemented the above algorithm to find the prime factors.

Users can try to find the unique prime factors of the large numeric values, such as 1020, and see that code gives an output without any error.

<html>
<body>
<h2>Solving the <i> Process out of memory </i> while finding all prime factors of a number in JavaScript</h2>
<div id = "output"> </div>
</body>
<script>
let output = document.getElementById('output');
function getPrimeFactors(value) {
let initial = value;
var factors = [];
for (let m = 2; m * m <= value; m++) {

// if the value is divisible by m, it is a prime factor
if (value % m === 0) {
factors.push(m);

// divide value by m until it is divisible
while (value % m === 0) {
value /= m;
}
}
}

// push largest prime factor at last
if (value > 2) {
factors.push(value);
}
output.innerHTML += "The prime factors of " + initial + " are : " + factors + "<br/>";
}
getPrimeFactors(100000000002);
getPrimeFactors(65416841658746141122);
</script>
</html>


## Example

In the example below, we have used the set to store the prime factors rather than using the array, as we need to get unique prime factors. Also, we have used the for-of loop to print all prime factors stored in the set.

<html>
<body>
<h2>Solving the <i> Process out of memory </i> while finding all prime factors of a number in JavaScript</h2>
<div id = "output"> </div>
</body>
<script>
let output = document.getElementById('output');
function getPrimeFactors(value) {
let initial = value;
var factors = new Set();
for (let m = 2; m * m <= value; m++) {
if (value % m === 0) {
while (value % m === 0) {
value /= m;
}
}
}
if (value > 2) {
}
output.innerHTML += "The prime factors of " + initial + " are : <br/>";

// print values of a set
for (let fac of factors) {
output.innerHTML += fac + "<br/>";
}
}
getPrimeFactors(44352747207453165);
</script>
</html>


We learned to solve the heap out of memory error while finding the prime factors of numeric value. Whenever we get an error like heap out of memory, we need to optimize our code as we have optimized in this tutorial.