473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

big binary string


hello,

I'm writing a script which needs about 60 kbytes of text data.
Currently it looks like:
function1("small string1");
function2("small string2");
function1("small string3");
and so on. In order to have it loaded more quickly, I'm considering
actually puting all the data in one big string (something like:
1<small string1>2<small string2>1<small string3>...
) and compress it (using a naive compression approach, I can
shrink the 60 kb down to approximately 20 kb, plus 2 or 3 lines of
javascript code to decompress the stuff). Now the question is:

QUESTION:
---------

Is it okay to have a statement like:

mystring = "blablabla\
blablabla\
blablabla";

with 20 kilo bytes of binary blablabla ? What characters may I use in
the string? (can I use any bytes from 1 to 255, or should I restrict
to the printable subset of ISO latin 1) ?

Thanks for any answers,

Laurent Vogel
Jul 20 '05 #1
4 4863
"Laurent Vogel" <lv*@club-internet.fr> writes:
Is it okay to have a statement like:

mystring = "blablabla\
blablabla\
blablabla";
No. Javascript strings cannot span lines. You should do it as:
var mystring = "blabla...bla"+
"blablabl.. .." +
"blablabl.. .." +
"blablabl.. .." +
"blablabl.. .."...
although with that many lines, pure concatenation is probably
inefficient (quadratic time complexity). I think a better solution is
to build an array and join it in one go:
var myarray = ["blabalbal...bla",
"blabablablabl",
"bablb",
...
"lbalblab"];
var mystring = myarray.join("");

with 20 kilo bytes of binary blablabla ? What characters may I use in the string? (can I use any bytes from 1
to 255, or should I restrict to the printable subset of ISO latin 1)?


Good question. That depends, among other things, on the encoding used
to send the page. ECMAScript says:
SourceCharacter ::
any Unicode character

StringLiteral ::
" DoubleStringCharacters_opt "
' SingleStringCharacters_opt '

DoubleStringCharacters ::
DoubleStringCharacter DoubleStringCharacters_opt

DoubleStringCharacter ::
SourceCharacter but not double-quote " or backslash \ or LineTerminator
\ EscapeSequence

so any Unicode character should work, if you can send it using the
encoding you use.

I would restrict myself to, e.g., ISO latin 1, and possibly the
printable subset. If you need codes outside of that, you can use
escapes: \xff (hex) or \0177 (octal), or for those that have an
escape, you can use it: \n. You probably don't want to use the unicode
escape, e.g., \u21e7.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
JRS: In article <3f***********************@news.club-internet.fr>, seen
in news:comp.lang.javascript, Laurent Vogel <lv*@club-internet.fr>
posted at Fri, 19 Sep 2003 07:49:20 :-
...
I can
shrink the 60 kb down to approximately 20 kb, plus 2 or 3 lines of
javascript code to decompress the stuff). Now the question is: Is it okay to have a statement like:

mystring = "blablabla\
blablabla\
blablabla";
AIUI, \ is not part of the standard; it may not always work.

with 20 kilo bytes of binary blablabla ? What characters may I use in
the string? (can I use any bytes from 1 to 255, or should I restrict
0 to 255 ? But you would need to escape " \ and CR/LF (& LS, PS, FF?),
at the very very least.
to the printable subset of ISO latin 1) ?


Using characters 33 to 126 should be safe. IIRC, MIME uses 64 of them,
storing 3 arbitrary bytes in 4 legible ones, and PostScript can use 85
of them, storing 4 in 5. No lesser expansion from fully-compressed
seems worth the effort.

BUT :

Over all links other than dial-up and long-distance radio, 60kB will not
take very long; and, AIUI, the lower-speed links are generally data-
compressed by hardware. Probably, if you know the structure of your
data, you can compress more tightly; but I wonder whether you can make a
gain sufficient to justify the effort?
H'mmm - IIRC, "Basic English" can say anything that needs to be said,
generally, with 850 simple words; and for special topics 150 special
words. So if the text is so written, you could send a word-list plus
for each string the numbers for each word. Whether French can be so
treated I do not know.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

Thanks for the answers.
I don't quite understand why using ASCII 32 (space) in
strings should cause any problem. Just to be sure I will
restrict myself to the range 32-126.
As for whether compression can be worth the trouble,
frankly I don't know, but I maintain that I'm able to
shrink approximately 60k into 20k of comressed data (plus
three lines added for the decompression code). As an
example here is the decompressor together with the
(compressed) description of the compression format used.
<html><head></head><body><script>
/* here is the uncompressing routine */
function u(s){var n,i=0,g=function(){if((n=s.charCodeAt(i++)-32)>=64)
n=(n-64)*95+s.charCodeAt(i++)-32;},o,d="",j=0;for(;;){g();d+=s.substr(i,
n);
i+=n;j+=n;g();if(0==(o=n))return d;g();while(n-- >0)d+=d.charAt(j++
-o);}}

/* and here is sample compressed data */
document.write(u([
"X<p>This is an example of compression for Javascript.
The@0#mat`d$(\nvery s",
"i`f$%, and5$'best de^$5bed by the algorithm N%)mented in=%#\nuna\")?ng
rout",
"ine:</p>\n\n<p>Conceptuall`n& P4Hg will read objects one at a time from
\n`",
"`$'\"input\"au&Ealways append data to a \"buffer\". Wheaj&#jobbE$!d`x$
az% ",
"D() containsa`/!e`q&!.b-)(Now here`g$ bz- bP(<blockquote><pre>forever
{\n ",
"b?&(<i>n</i>1$ av' 3(/ bytes verbatimbT& bS+% into1& az' `v,&offset`h%
7$'q",
"uit if:/$== 0aLK&copiedaJ& `i. @& aP'+current end`l$! fz$ ah, az2',
allowe=",
"$(overlappeQ%&egionsb#%' (i.e.a9/$&lt;ag)%)\n}</cj$\"</c|+\"\n
dO%Lumbers ",
"smaller than 64 are encoded as ASCII /$% 32 +af%!n[%*. Bigger
\n0&!s[-!ugC%",
"3two printable ascii^'#s (c9% a!.1to \n126 included)ii& i]* i\\%-crude,
but",
" itc,&#s a<& b1% i<0 a $%. Notg_$ hZ$ c_< j 3\"onj (4 provides run
lengthbV",
"& `c$ kg$$freeh9*!Ol)$)urse, perb*%\"nchG% ff$(e poor wiJ$ a.$)ared
withff$",
"!llC)-ors\nlike gzipbZ&%honorcX% af-2atio can be achiev`q$ jJ( b5+ j$'
jC**",
"many repealf$*substringsb-& "].join("")));
</script></body></html>
regards,

Laurent Vogel
-- remove "ima" and "ictor" to get my email address

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
On 21 Sep 2003 18:52:09 GMT, Laurent Vogel
<Li************@club-internet.fr> wrote:

Thanks for the answers.
I don't quite understand why using ASCII 32 (space) in
strings should cause any problem. Just to be sure I will
restrict myself to the range 32-126.
As for whether compression can be worth the trouble,
frankly I don't know, but I maintain that I'm able to
shrink approximately 60k into 20k of comressed data (plus
three lines added for the decompression code). As an
example here is the decompressor together with the
(compressed) description of the compression format used.
<html><head></head><body><script>
/* here is the uncompressing routine */
function u(s){var n,i=0,g=function(){if((n=s.charCodeAt(i++)-32)>=64)
n=(n-64)*95+s.charCodeAt(i++)-32;},o,d="",j=0;for(;;){g();d+=s.substr(i,
n);
i+=n;j+=n;g();if(0==(o=n))return d;g();while(n-- >0)d+=d.charAt(j++
-o);}}


Have you done performance testing of compressing data like this vs.
using uncompressed data over various link speeds? I'd be very wary of
the above. String concatenation is one of the slowest operations you
can do in Javascript due to naive memory allocation, and when you
start concatenating large strings repeatedly in a loop... Well, lets
just say that we used to have some scripts that did that. We were
dealing with data sizes up in the 200-400K range and the script took
15 minutes to run. After reducing the number of string concatenations
where one of the two operands was very large the script only took 2.5
minutes. You need to test whether this is worthwhile. I believe John
is correct and it will not be.

Regards,
Steve
Jul 20 '05 #5

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

Similar topics

1
by: Ferran Foz | last post by:
Hello, I'm using ADODB.Stream to open a binary file on the server and write it down to the browser using Response.BinaryWrite. It's working fine, but i need to make some changes to the binary...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
10
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; ...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
2
by: Edvin | last post by:
Why is binary array written to a file different than when converting the binary array to string and then writing it to file! For example: // --- Allocate byte array byte arrByte = new byte;...
8
by: Lucas | last post by:
I need print a file in binary mode . f = f.open('python.jpg','rb') bytes = f.read() f.close() print(bytes) I can't get any binary code.
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
3
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX...
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,...
1
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.