By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

Max allowed length for a javascript string?

P: n/a
Can't find it quickly in the reference books.

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> Can't find it quickly in the reference books.

The ECMAScript Programming Language Specification does not specify a maximum
length. The maximum length with will be implementation-specific. For some
implementations, it will be a function of available memory, but you should not
rely on that if portability is important.

http://www.ecmascript.org

Jul 20 '05 #2

P: n/a
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:9a**************************@msgid.meganewsse rvers.com...
<snip>
... . The maximum length with will be implementation-specific. ...


In microsoft.public.scripting.jscript, Michael Harris
(Microsoft.MVP.Scripting), who might be expected to know, quoted:-

<quote Message-ID: <#C**************@TK2MSFTNGP12.phx.gbl>
In JScript, variant string variables have the same limit as in VBScript,
up to 2^31 characters.

String *literals* (as in "this is a literal") have (IIRC) a limit ~2^10
(1024) characters.
</quote>

- for the JScript implementation.

Richard.
Jul 20 '05 #3

P: n/a


Richard Cornford wrote:

I can tell from experience it's not 1024. The last string I used was over
23K bytes. However, I just wanted to make sure the limit wasn't something
like 2^16, which I'll collide with shortly. It's just a packing script
to export to MS Access from webdom. Since javascript has C as it's
progenitor, that's probably the limit. I've forgotten that limit, and
there's no book handy.

Wade
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:9a**************************@msgid.meganewsse rvers.com...
<snip>
... . The maximum length with will be implementation-specific. ...


In microsoft.public.scripting.jscript, Michael Harris
(Microsoft.MVP.Scripting), who might be expected to know, quoted:-

<quote Message-ID: <#C**************@TK2MSFTNGP12.phx.gbl>
In JScript, variant string variables have the same limit as in VBScript,
up to 2^31 characters.

String *literals* (as in "this is a literal") have (IIRC) a limit ~2^10
(1024) characters.
</quote>

- for the JScript implementation.

Richard.


Jul 20 '05 #4

P: n/a
"Wade G. Pemberton" <w_*********@sprynet.com> writes:
I can tell from experience it's not 1024. The last string I used was over
23K bytes.
It is highly browser dependent.

I just created a string literal in IE 6 that was 2^20 (~1M) characters
long. It took a while to parse! I also created a string that was 2^24
characters long. Then I wrote it as a string literal, and it hasn't
finished parsing it yet.

Mozilla breaks between 2^15 and 2^16 (32768 and 65536).

Opera 7.21 breaks somewhere between 2^11 and 2^12 (2048 and 8192).
[2^16?] Since javascript has C as it's progenitor, that's probably the
limit.


C is not Javascript's progenitor any more than Java is. All they share
is the syntax.

C has no inherent limits, although most implementations have a size_t
that is 32-bit.

/L 'please trim your quotes'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
Why not just test it?

String.prototype.repeat = function(n) {
return new Array(n + 1).join(this);
}
var s = "x".repeat(Math.pow(2, 23)) + "y";
document.write(s.charAt(s.length - 1));

Anything larger in IE and I get an Out of memory error trying to *construct* the
string, but that's probably due to the implementation I'm using (constructing
the array and using .join()). I was able to get Firebird up to
..repeat(Math.pow(2, 22)) before it completely gave up the ghost and stopped
responding. Interestingly enough, Opera 7.21 seemed particularly adept at this
task, and was able to construct a string 2^31 long almost instantly.

As for any limit in C having an effect on a limit in JavaScript, that's silly. A
String in JavaScript is an object containing a set of unicode characters, as
well as various properties and methods, isn't a string in C simply an area of
memory at begun at some location, and terminated by a \0? So why should there be
an artificial limit in C at all?

Considering the modern browsers I tested were capable of setting and retrieving
the last character of a string at least 2^20 long, I doubt you'll have any
problems for the foreseeable future.

"Wade G. Pemberton" wrote:
Richard Cornford wrote:

I can tell from experience it's not 1024. The last string I used was over
23K bytes. However, I just wanted to make sure the limit wasn't something
like 2^16, which I'll collide with shortly. It's just a packing script
to export to MS Access from webdom. Since javascript has C as it's
progenitor, that's probably the limit. I've forgotten that limit, and
there's no book handy.

Wade
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:9a**************************@msgid.meganewsse rvers.com...
<snip>
... . The maximum length with will be implementation-specific. ...


In microsoft.public.scripting.jscript, Michael Harris
(Microsoft.MVP.Scripting), who might be expected to know, quoted:-

<quote Message-ID: <#C**************@TK2MSFTNGP12.phx.gbl>
In JScript, variant string variables have the same limit as in VBScript,
up to 2^31 characters.

String *literals* (as in "this is a literal") have (IIRC) a limit ~2^10
(1024) characters.
</quote>

- for the JScript implementation.

Richard.


--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.