Javascript text compression / decompression

Suppose I have a 400 KB text file that I want to read from javascript. The problem is that my target audience has a slow connection, so 400k may take too long.

I suppose I need to compress the file, but well, how can I unzip it using javascript on the client side?

Is it worth it, or does the time needed for decompression negate the time saved at boot?

UPDATE

To be clear, a file is text (data), not code.

+4
source share
6 answers

You can gzip a text file and send it to your browser. Thus, you do not have to do anything on the client side, the browser itself will unpack it.

+8
source

Can I use HTTP compression?

+1
source

It looks interesting: http://rumkin.com/tools/compression/compress_huff.php

Several tests with a lot of text showed a good result.

+1
source

[An old question, but just in case other people stumble over it ...]

The best way to handle this is to use HTTP compression . All major web servers and web browsers support this. In fact, if you use a web hosting service, and your file is a type of file that usually requires compression (for example, css, js, html), then it is probably already compressed for transport, and you just don’t know about it.

0
source

Yes, you do not need to handle compression / decompression, so the browser for you only you need to specify the server parameter for the server that you are using. Note: you can explicitly indicate why all MimeType (response format) Ex.

The following is the tomcat server configuration parameter.

<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true" maxThreads="200" compression="on" compressionMinSize="2048" compressableMimeType="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,charset=UTF-8,application/x-www-form-urlencoded,text/html,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json" /> 

To verify that compression works, you can check. press F12 (developer tool) [my Chrome] β†’ "Network" tab β†’ start recording. click the URL of the request that responds with the required data / text. Then go to the header tab.

you will see.

REquest / REsponse Headers

 Content-Encoding:gzip Accept-Encoding: gzip, deflate 

that he uses encoding. and the size column on the network tab againsr url will show a reduced size.

0
source

DECOMPRESS JAVASCRIPT

It is IMPOSSIBLE to hide compressed code.

Typical JavaScript compressed with / packer / starts with the following code:

  `eval(function(p,a,c,k,e,r)`… `eval` can simply be replaced by `alert`. 

The eval function evaluates a string argument containing JavaScript. Most packers use eval and then document.write .

To unpack JavaScript, replace these methods with one of the following:

1. Replace eval with alert (For example: alert(function(p,a,c,k,e,r) ... and open or refresh the html page, the warning will simply print the code in a pop-up window)

2. If JavaScript appears after the <body> element, you can add <textarea> like this:

  `<textarea id="code"></textarea>` 

Then replace eval (...); on document.getElementById("code").value=…;

Of both options, the first is simpler ... Good luck :)

-3
source

Source: https://habr.com/ru/post/1286215/


All Articles