Javascript: How to Fail Memory Allocation Gracefully?

Hello.

I have written several Javascript programs that accept user input (numbers) from a textarea box. The data then gets converted to floating point numbers for use in the actual programs.
As far as I know, the general procedure follows:
Step 1: Any data in a textarea box is by default one big string so it must be input to the Javascript program for processing as a string. Call this string TEXTAREABOXSTRING
Step 2: Split the string (separate the numbers by whitespace) up into the individual entries, placing the individual entries into a raw array of strings. Call this array RAWARRAYOFSTRINGS
Step 3: Convert all the array entries into numbers and place them in the proper numeric array for further use in the program. Call this array NUMERICARRAYTHATWILLBEUSEDINPROGRAM

Up to now, all the programs have explicitly enforced an upper limit as to the size of the numeric array that the program will accept. There is a statement on the HTML page telling users about this limit, and in the Javascript code itself there is a check to ensure the numeric array is no larger than the specified limit.

However, this check only happens at Step 3, when NUMERICARRAYTHATWILLBEUSEDINPROGRAM is filled.

I would like to try something different; I would like to start using dynamic arrays. However, I am wondering what checks to include to ensure the memory allocation can be performed. And if it cannot, how do I code the program so that it fails gracefully?

For example, say a user tries to deliberately break the program. He copies and pastes a HUGE block of numbers from an e-book. Some scenarios:
(i) TEXTAREABOXSTRING is so huge, the user doesn’t have enough memory and the program fails.
(ii) The program accepts TEXTAREABOXSTRING okay, but there is not enough memory for both TEXTAREABOXSTRING and RAWARRAYOFSTRINGS. So the program fails at Step 2.
(iii) The program accepts TEXTAREABOXSTRING and RAWARRAYOFSTRINGS okay, but there is not enough memory for TEXTAREABOXSTRING, RAWARRAYOFSTRINGS, and NUMERICARRAYTHATWILLBEUSEDINPROGRAM. So the program fails at Step 3.
In fact, triple the memory is being used for the same data even though it is only the numeric array that will be used in the program (once it is filled).
Is there a way to de-allocate memory being used by TEXTAREABOXSTRING? It is not going to be used later in the program and I would like to reduce the chance that a memory allocation error might happen later.

What can I do to ensure memory allocation failures happen gracefully?
(Should I even worry about it, or is it handled internally by the browser?)

About the only thing that you can do in that case is to assign an empty string to that text area box.

Garbage collection is automatically handled by the browser. There isn’t much that JavaScript provides to deal with such issues.

Okay. Thanks for the info.

I am accustomed to coding in C++, so usually wrap memory allocation blocks in a try block, and catch exceptions in a catch block. I thought there might be some similar steps that can be taken in JavaScript.

Good suggestion, assigning an empty string to TEXTAREABOXSTRING once it has been broken up into its individual entries and saved in RAWARRAYOFSTRINGS.
That way, I am not using up twice the memory saving what is essentially the same data in two places.

I suppose I could also delete RAWARRAYOFSTRINGS one entry at a time as the entries are converted to numbers and saved in NUMERICARRAYTHATWILLBEUSEDINPROGRAM.
For example, I could loop through RAWARRAYOFSTRINGS as follows:

for (var i = 0; i < RAWARRAYOFSTRINGS.length; i++){
     NUMERICARRAYTHATWILLBEUSEDINPROGRAM[i] = parseFloat(RAWARRAYOFSTRINGS.shift());
}

That way, as one array grows the other shrinks (I think). Again, I can avoid using up twice the memory saving what is essentially the same data in two places.

JavaScript does have try and catch blocks just as C++ does.