1

in some other post I found that there should be limitation like 512mb (~2^27) for the maximum string length in javascript. I have the following code in my application:

i = 1;
selectionField.value = i;
i++;
for (i;i<=tableEntries;i++){
selectionField.value = selectionField.value + "," + i;
}

whereby "tableEntries" is e.g. "40000". This code executes fine in Firefox (though it takes around 5-10 seconds and prompts with a warning for a long running script) but does not execute in chrome. Chrome just interupts in this loop and says it cannot display the page. Is there a particular setting in chrome that can lead to this unexpected result? I mean the final string should have a length of ~240k chars. This should be fine according to the limit mentioned at the beginning.

Thanks for help in advance!

Tobias S.
  • 25
  • 1
  • 1
  • 6

1 Answers1

1

The problem is not with the length of the string, but that you're trying to update the value in the input field on screen 240k~ times.

A better solution would be to copy the field value to a variable, perform the string concatenation in-memory and then update the finished string only once in the DOM (on screen).

Adding to your code, it would be something like this:

i = 1;
var str = i;
i++;
for (i;i<=tableEntries;i++){
str = str + "," + i;
}
selectionField.value = str;
Orr Siloni
  • 1,268
  • 10
  • 21