4

Suppose I have a 400K text file which I want to read from a javascript. The problem is, my target audience have a slow connection, so 400k might take too long to load.

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

Is it worth it, or will the time needed for decompression negate the time saved in downloading?

UPDATE

Just to be clear, the file is text (data) not code.

hasen
  • 161,647
  • 65
  • 194
  • 231
  • 1
    Well, it would depend on what the text file contains! Basically, the compression will be removal of extra spaces and line breaks, which can decrease the file size substantially, but how will you re-construct it again on the client side? – Kirtan Jun 15 '09 at 06:10

7 Answers7

8

You can GZip the text file, and sent it to the browser. That way you wont have to do anything on the client side, the browser itself will decompress it.

Kirtan
  • 21,295
  • 6
  • 46
  • 61
1

could you use HTTP compression?

Bluephlame
  • 3,901
  • 4
  • 35
  • 51
1

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

A few tests with a LOT of text turned up a pretty good result.

Travis
  • 1,872
  • 1
  • 16
  • 12
  • I just tried it, it increased the size rather than decreasing it – hasen Jun 15 '09 at 06:30
  • Yeah, on small amounts of text is does increase the size but when you put a few pages of random text in there it compresses it pretty well. I'm seeing 30-50% compression. – Travis Jun 15 '09 at 07:01
  • If you want a do-it-yourself compression ( not the real solution, given by @Bluephlame ), see the marvellous javascript LZW at http://140byt.es/ – Massimo Nov 27 '11 at 16:00
0

[Old question, but just in case other people stumble across this...]

The best way to deal with this is to use HTTP compression. All major web servers and web browsers support this. In fact, if you're using a web hosting service and your file is a filetype that commonly requires compression (e.g. css, js, html), then it's probably already being compressed for transport and you're just not aware of it.

broofa
  • 37,461
  • 11
  • 73
  • 73
0

Yes you don't not have to handle compression/decompression yourself browser does it for you only you have to specify server parameter's for the server that you are using. Note: you can explicitly specify for what all MimeType( response-format's) Ex.

Below is tomcat server setting 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 be sure that the compression is working you can check. press F12(developer's tool)[mine is Chrome] -> Network tab -> start recording. click the request url that is responding the required data/text. Then go to the header's tab.

you will see.

REquest/REsponse Headers

Content-Encoding:gzip

Accept-Encoding: gzip, deflate

that it using encoding. and the size column in the network tab againsr the url will be showing the reduced size.

Arun Pratap Singh
  • 3,428
  • 30
  • 23
-1

[very old question, but still showing up in searches]

I ended up writing a small, self-contained library for the task. It compresses/decompresses JSON data to a string in the printable ASCII range. The size of the minified decompressor is 2410 bytes, and 3177 bytes for the compressor.

https://github.com/TGlas/json-compression

tglas
  • 949
  • 10
  • 19
-3

DECOMPRESS JAVASCRIPT

Is IMPOSSIBLE to hide the compressed code.

A 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 that contains JavaScript. In most packers, eval is used, followed by document.write.

To decompress JavaScript, replace these methods by one of the following:

1. Replace eval by alert (Like this: alert(function(p,a,c,k,e,r)and open or refresh the html page, the alert will simply print the code in a popup-window)

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

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

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

From both options the first is more simple... Good luck :)