[Javascript] JavaScritpt simple compression code

Allard Schripsema allard-schripsema at procergs.rs.gov.br
Fri Feb 18 07:53:56 CST 2005


To fix the string performance you should first measure the time of your
loops
var now1 = new Date()
your LOOOOOOOOOOOOOOOOOOOOOOP
var now2 = new Date()
alert(now2.getTime()-now1.getTime())
Having this time, you will know is your change are making things better or
worse

Then you must understand that every concatenation on an existing string will
take more time than the previous one, because of the memory increase.
That means that in certain situations (big strings) it is much faster to
create groups of small strings (an array of strings), concatenating them
only in the end.

Knowing this you can make your loops to get more speed doing something like
this
(code not tested)

cachesize=30 //test different sizes
cacheIndex=0 //will count noCaches
var cache= new array()
noElements = array_a.length
var start = 0
var leftover= = noElements%cachesize
if (leftover>0){
	do{
  		cache[cacheIndex]+="&" + elem + "=" + array_a[start];
  	}while (++start < leftover)
}
cacheIndex=cacheIndex+1
while (noElements>cacheIndex*30+leftover){
	for (var counter = 1 to cachesize){
 		cache[cacheIndex] += "&" + elem + "=" +
array_a[cacheIndex*30+counter+leftover]; }
	}
	cacheIndex=cacheIndex+1
}

Then concatenate the caches
for( var elem in cache{ mytext += cache[elem]; }


AND start measuring again and play with the cachesize until you found your
best value.

that?s it ...
Good luck!

Allard schripsema
-----Original Message-----
From: javascript-bounces at LaTech.edu
[mailto:javascript-bounces at LaTech.edu]On Behalf Of Eligio Morgado
Sent: Friday, February 18, 2005 10:40 AM
To: [JavaScript List]
Subject: Re: [Javascript] JavaScritpt simple compression code


Hi Allard.

First of all, as you can see my English is not very good. I don't
understand very well your question.

My JavaScript code has many different files with many classes, arrays, etc.

I need to save this information in a DB because when you re-enter the
page you can load previous data.

So, I build a string concatenating all arrays data, class data, etc.

You are correct, that part of the code is quite slow, until geting the
full string.

Imagine:

var mytext = "";

for( var elem in array_a ) { mytext += "&" + elem + "=" + array_a[elem]; }
for( var elem in array_b ) { mytext += "&" + elem + "=" + array_b[elem]; }

And this continues for every data I need to persist.

Debuging the code, I see that forming the string it's a slow process
(as you say). But it's slowler when GETs started to be sent.




On Fri, 18 Feb 2005 10:23:43 -0200, Allard Schripsema
<allard-schripsema at procergs.rs.gov.br> wrote:
> Hi there
>
> First of all i?d like to ask you if you are concatenating (using a loop)
to
> get these big strings
> like for (...) {strchunk =getChunk() ; strtotal = strtotal + strchunk }
>
> If this is the case, this probably is your big performance problem, NOT
the
> sending of these strings. (even 50 querystrings = 100 tcp packets * 20
ms???
> NOT a "big" deal)
> The answer would be to avoid concatenating into the big string until the
> latest possible moment
>
> If you?re after some simple compression tecnique, check out the .pcx
> (imageformat) compression.
> Or we could invent something here in this group, if you want.
>
> great problem!
>
> Allard Schripsema
>
>
> -----Original Message-----
> From: javascript-bounces at LaTech.edu
> [mailto:javascript-bounces at LaTech.edu]On Behalf Of Eligio Morgado
> Sent: Friday, February 18, 2005 10:09 AM
> To: [JavaScript List]
> Subject: Re: [Javascript] JavaScritpt simple compression code
>
> Hi Matt.
>
> Testing with IE, I found that I can send URLs of less than 2000
> characters. So I split original string in 1900 length parts (I don't
> like to reach the exact limit).
>
> The largest string I have found is about 200.000 characters.
> Fornutatly, normally they are about 1000-5000 (sometimes I only GET
> one time).
>
> Don't mind for strings larger than 200.000. This is a very specific
> scenary and I can create a rule for not sending strings larger than
> 250.000, for example.
>
> In response to POST vs GET, I found that with default IE configuration
> user gets a security message when trying to submit the hidden form.
> That's why I disagree with POST.
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list