473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

string length question

Hi;

Whats the maximum string length in Javascript.

Is it the same across all the browsers?

max_s_len = str.length // what the max number of chars we can have is
str?

Regards
Ian

Jul 10 '06 #1
3 7394
ip*****@pipemedia.co.uk wrote:
Whats the maximum string length in Javascript.
Are you asking about the value of a string primitive or the maximum
length of a string literal in source code? I have never seen stated
values for any maximum length of a string primitive's value (so
probably limited by available memory), and have seen practical tests
going into the hundreds of millions of characters (so probably
exceeding all practical needs). Microsoft used to claim a size limit
for a string literal token, but previous discussions on the subject
proved that the stated figure no longer applied and that the current
real figure is so large that nobody wanted to waste the time necessary
to find out what the limit was.
Is it the same across all the browsers?
There is no reason to expect any limit to be the same across browsers
except by coincidence (such as would result from using a 32bit integer
value to hold the length in different implementations). If in practice
the limiting factor tends to be available memory then all browsers on
the same machine will have the same available memory.
max_s_len = str.length // what the max number of chars we can
have is str?
If you really must know you can carry out your own tests. You will find
that as the strings get bigger the code slows down so much that you
will get fed up with waiting before you find a definitive answer.

Richard.

Jul 10 '06 #2
ip*****@pipemedia.co.uk writes:
Whats the maximum string length in Javascript.
The specification does not define a maximal length ...
Is it the same across all the browsers?
.... so it's unlikely that all implementations have the same limits.
max_s_len = str.length // what the max number of chars we can have is
str?
If we insist that all lengths must be expressible exactly as a number,
then the limit will be 2^53. At one byte per character (which is
probably too low), that requires ~8 petabyte of memory. So this
theoretical limit is unlikely to be the limiting factor.

A more likely limit is the range of an UInt32.

A quick script to check the limit (although probably not in reasonable
time):

var N = 32; // vary this and see if it works.
var x = "x";
for(var i = 0; i < N; i++) { x= x+x; };
alert(x.length)

Try this in different browser, starting with a lower N and see when
it breaks :)

In practice, my guess is that long strings take much memory and time
to manipulate, so you'll want to make them shorter long before you
reach a hard limit.
/L
--
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 10 '06 #3
JRS: In article <d5**********@hotpop.com>, dated Mon, 10 Jul 2006
14:38:09 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lr*@hotpop.composted :
>ip*****@pipemedia.co.uk writes:
>Whats the maximum string length in Javascript.
>If we insist that all lengths must be expressible exactly as a number,
then the limit will be 2^53. At one byte per character (which is
ECMA requires 16-bit units, but not that they are characters.
>probably too low), that requires ~8 petabyte of memory. So this
theoretical limit is unlikely to be the limiting factor.

A more likely limit is the range of an UInt32.
Some routines use -1 or lower, which could change that to SInt32.

Some implementations seem likely to use DWORD internally, imposing a
limit at 2^32-1 bytes.
>A quick script to check the limit (although probably not in reasonable
time):

var N = 32; // vary this and see if it works.
var x = "x";
for(var i = 0; i < N; i++) { x= x+x; };
alert(x.length)

Try this in different browser, starting with a lower N and see when
it breaks :)
Or put the alert inside the loop, changing it to a Confirm, and continue
the loop until the computer or the user gives out.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tim Simmons | last post by:
I am stumped. I encoded the action = of my form using GET and I can't seem to get the property/value stuff from it using a JavaScript script I got from the web. I want to create a trivia game...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
7
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
7
by: junw2000 | last post by:
In the following code: std::string s1; s1 = "ABCDE"; std::cout<<s1<<std::endl; s1 = '\0'; //LINE1 std::cout<<s1<<std::endl; //LINE2 At LINE1, I add a NULL terminator...
17
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: David Hirschfield | last post by:
I'm implementing a relatively simple inter-application communication system that uses asyncore/asynchat to send messages back and forth. The messages are prefixed by a length value and terminator...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.