473,397 Members | 1,950 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,397 software developers and data experts.

Internal string storage in JavaScript?

VK
Does anyone have a reputable reference about internal string storage in
JavaScript? (for some particular implementation I mean).

Say having 1,048,576 characters long string from the geometric
progression:

function generateLargeString() {
var s = 'a';
for (var i=1; i<21; ++i) {
s = s.concat(s);
}
return s;
}

- the internal size should be 2 mebibytes and not 1 (?) if strings are
indeed stored as Unicode 16-bit. From the other hand it would be
tempting for an engine developer do not spend extra bytes on ASCII
chars...

So does anyone know of any documented engine optimizations on the
matter? Would be expected on some engine to have the string from above
twice smaller than say

function generateLargeString() {
// 1200 ETHIOPIC SYLLABLE HA
var s = String.fromCharCode(0x1200);
for (var i=1; i<21; ++i) {
s = s.concat(s);
}
return s;
}

Dec 14 '06 #1
2 2414
VK wrote:
Does anyone have a reputable reference about internal string storage in
JavaScript? (for some particular implementation I mean).
I suppose you actually mean RAM with "internal string storage". Well,
the code for Open Source browsers such as Opera/Firefox is available.
Not an easy subject, I'ld say, and it's probably quite tough to
understand those primitive mechanisms if you're not familiar with them.
Say having 1,048,576 characters long string from the geometric
progression:

function generateLargeString() {
var s = 'a';
for (var i=1; i<21; ++i) {
s = s.concat(s);
}
return s;
}

- the internal size should be 2 mebibytes and not 1 (?) if strings are
indeed stored as Unicode 16-bit.
You can't put a number like 1 or 2 MB on the required "internal size"
(=RAM!). It heavily depends on the js engine, O.S., intermediary
levels... because javascript is such a high-level language. The
required RAM for a variable that holds 1,024,576 characters will always
be more than 1MB (and I would suspect quite a lot more than that).
From the other hand it would be tempting for an engine developer do
not spend extra bytes on ASCII chars...
Those bytes are "spent" in javascript, because each character is parsed
as its full Unicode code point.
So does anyone know of any documented engine optimizations on the
matter? Would be expected on some engine to have the string from above
twice smaller than say

function generateLargeString() {
// 1200 ETHIOPIC SYLLABLE HA
var s = String.fromCharCode(0x1200);
for (var i=1; i<21; ++i) {
s = s.concat(s);
}
return s;
}
My tests seem to confirm that ASCII or not doesn't matter, which is
actually logic, because javascript uses Unicode internally for every
character. My benchmarks on MSIE 6.0.29 (WinXP):

document.write(Date()+' - ')
s="a"
for (var i=1; i<100000; ++i) {
s+="a"
}
document.write(Date())

Thu Dec 14 12:44:15 2006 - Thu Dec 14 12:44:18 2006
Thu Dec 14 12:44:34 2006 - Thu Dec 14 12:44:37 2006
Thu Dec 14 12:45:24 2006 - Thu Dec 14 12:45:27 2006
Thu Dec 14 12:45:39 2006 - Thu Dec 14 12:45:42 2006
Thu Dec 14 12:45:56 2006 - Thu Dec 14 12:45:59 2006
Thu Dec 14 12:46:06 2006 - Thu Dec 14 12:46:09 2006
Thu Dec 14 12:46:17 2006 - Thu Dec 14 12:46:20 2006

document.write(Date()+' - ')
u="\u0945"
for (var i=1; i<100000; ++i) {
u+="\u0945"
}
document.write(Date())

Thu Dec 14 12:47:20 2006 - Thu Dec 14 12:47:23 2006
Thu Dec 14 12:48:01 2006 - Thu Dec 14 12:48:04 2006
Thu Dec 14 12:48:13 2006 - Thu Dec 14 12:48:16 2006
Thu Dec 14 12:48:24 2006 - Thu Dec 14 12:48:27 2006
Thu Dec 14 12:48:33 2006 - Thu Dec 14 12:48:36 2006
Thu Dec 14 12:48:50 2006 - Thu Dec 14 12:48:53 2006
Thu Dec 14 12:49:06 2006 - Thu Dec 14 12:49:09 2006

on FireFox 1.0.4. (mind the 1000000 in stead of the 100000, which makes
Firefox much faster than MSIE in this regard!):

document.write(Date()+' - ')
s="a"
for (var i=1; i<1000000; ++i) {
s+="a"
}
document.write(Date())

12:53:21 - 12:53:25
12:53:43 - 12:53:46
12:54:00 - 12:54:04

document.write(Date()+' - ')
u="\u0945"
for (var i=1; i<1000000; ++i) {
u+="\u0945"
}
document.write(Date())

12:50:29 - 12:50:32
12:51:03 - 12:51:06
12:51:48 - 12:51:51

I abbreviated Firefox' date strings for readability purposes.

--
Bart

Dec 14 '06 #2
In comp.lang.javascript message
<11*********************@16g2000cwy.googlegroups.c om>, Thu, 14 Dec 2006
04:17:48, Bart Van der Donck <ba**@nijlen.comwrote:
My benchmarks on MSIE 6.0.29 (WinXP):
If you store a starting Date Object, and after the timed part subtract
that from a new Date Object, the results will be in milliseconds and far
easier to compare.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Dec 14 '06 #3

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

Similar topics

4
by: Locusta | last post by:
Hello, I have been struggeling for replacing a string in a string. The snippet from the program below replaces the <, & and > with the XML equivalent values. In the program, I allocate space...
1
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal...
0
by: Itai | last post by:
I require implementing an internal messaging sub-system in asp.net website where users send messages to each other using their userid for the address. No SMTP / POP3 functionality required except...
32
by: santosh | last post by:
In following code char str = "asdf" ; str = 's' ; is possible. But char *str = "asdf" ; str = 's' ; is an run-time error. What i understand is *str = "asdf" is stored in Read-only-Memory....
23
by: metaosp | last post by:
Hi, in the following code: string foo("foo"); foo.resize(10); foo.append("bar"); cout << foo << endl; // output is "foo" I expect the output to be "foobar", although "bar" is also
4
by: gamehack | last post by:
Hi all, I've been designing a program which would need to have a function to replace values in a string. For example I could have a string: "<circle x="%" y="%">" and then I would replace the %...
15
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
21
by: Ray Cassick | last post by:
I can't believe why I had not noticed this before and why I never asked it before... When defining a string why is it not required to use the new keyword? Like: Dim a As String = New String ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.