Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22419

Re: Compressing js file?

From emf <emfril@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Compressing js file?
Date 2013-12-26 21:56 -0500
Organization Aioe.org NNTP Server
Message-ID <l9iq8h$hsi$1@speranza.aioe.org> (permalink)
References <l9a32q$vkf$1@speranza.aioe.org> <9b7e78ae-b456-42bb-bab4-d5ac2d10650b@googlegroups.com>

Show all headers | View raw


On 2013-12-23 19:40 Michael Haufe (TNO) wrote:
> On Monday, December 23, 2013 1:31:41 PM UTC-6, emf wrote:
>> Since Java has security problems and soon I might have to buy a
>> certificate, I decided to convert my Natal Transits Calculator Java
>> program to JavaScript:
>>
>> Java program, calculating 5 planets:
>>
>> https://files.nyu.edu/emf202/public/jv/nataltransits.html
>>
>> JavaScript program, calculating 2 planets (page still under construction):
>>
>> https://files.nyu.edu/emf202/public/nt/natalTransits.html
>>
>> The issue is this:
>>
>> The programs use a large ephemeris file. The size of the original
>> ephemeris.csv with 5 planets is 2.141 KB. It's in the form of
>>
>> 19000101,01i08,27i43,10i08,25c13,15c15
>> 19000102,01i20,27i50,10i12,25c12,15c14
>> ...
>>
>> The size of the jar file that includes the compressed ephemeris.csv, the
>> program files, and the code files is only 735 KB!
>>
>> The size of the ephemeris.js with only 2 planets is 1,606 KB. It
>> includes the ephem array that is in the form:
>>
>> var ephem = [["19000101","27i43","10i08"], ["19000102","27i50","10i12"],
>> ... ];
>>
>> The choice of including only 2 planets was *not* made mainly on file
>> size considerations. And BTW, with the JavaScript program I was able to
>> use the Unicode sign symbols, something problematic with the Java
>> program. (I hope everybody out there can see them.)
>>
>> My question is: Is there a way to make the data file smaller and still
>> be able to read it with the JavaScript program?
>>
>> And by smaller, of course, I do not mean eliminating the spaces between
>> the elements of the array that would save only 3%.
>
> I'd suggest leaving the data in csv form. On the fly, parse the
> desired elements using a regular expression to get its components.
> Alternatively|additionally, use gzip.

I had read about AJAX and reading files, but had not payed attention to 
its possibilities and had managed to forget about it. Using a csv file 
is of course better since it's 1178 KB while the js was 1606 KB. (I have 
uploaded it at https://files.nyu.edu/emf202/public/nt/ephemeris.csv). 
I'll come to the gzip and other issues at a later point.

For now I've written a test webpage to access a local csv file. The code is:

<!DOCTYPE html>
<html>
   <head><meta charset="utf-8" />
     <title>Read File</title>
     <script type="text/javascript">

       var reader, ephem = [];
       reader = new XMLHttpRequest();

       function loadFile() {
         reader.open('get', 'ephemeris.csv', true);
         reader.send(null);
       }

       function leapDay(yyyy) {
         var lDay;
         lDay = ((yyyy % 4 === 0 && yyyy % 100 !== 0) || yyyy % 400 ===
           0) ? 1 : 0;
         return lDay;
       }
       function isValid(yyyy, mm, dd) {
         var valid;
         if (yyyy > 0 && mm > 0 && mm <= 12 && dd > 0 && dd <= 31
           && !(dd === 31 && (mm === 4 || mm === 6 || mm === 9 ||
           mm === 11)) && !(mm === 2 && dd > 28 + leapDay(yyyy))) {
           valid = true;
         } else {
           valid = false;
         }
         return valid;
       }

       function findBDay() {
         var yyyy, mm, dd, yyyyS, mmS, ddS, birthday, i;
         yyyy = +document.dataForm.yField.value;
         mm = +document.dataForm.mField.value;
         dd = +document.dataForm.dField.value;
         if ( ((yyyy < 1900) || (yyyy > 2049)) || (!isValid(yyyy, mm,
           dd)) ) {
           i = -1;
         } else {
           yyyyS = yyyy.toString();
           mmS = (mm < 10) ? "0" + mm : mm;
           ddS = (dd < 10) ? "0" + dd : dd;
           document.dataForm.mField.value = mmS;
           document.dataForm.dField.value = ddS;
           birthday = yyyyS + mmS + ddS;
           //alert("Press OK to continue.");
           for (i = 0; i < ephem.length; i += 1) {
             if (birthday === ephem[i][0]) {
               break;
             }
           }
         }
         return i;
       }

       function ephemArray(data) {
         var i, j, birthday, saturn, uranus;
         for (i = 0; i < data.length / 22; i += 1) {
           ephem[i] = [];
         }
         for (i = 0; i < data.length / 22; i += 1) {
           j = i * 22;
           birthday = data.substr(j, 8);
           saturn = data.substr(j + 9, 5);
           uranus = data.substr(j + 15, 5);
           ephem[i][0] = birthday;
           ephem[i][1] = saturn;
           ephem[i][2] = uranus;
         }
       }

       function loadData() {
         var data;
         if(reader.readyState === 4) {
           data = reader.responseText;
           ephemArray(data);
         }
       }

       function findBirthData() {
         var bDay;
         loadFile();
         reader.onreadystatechange = loadData;
         bDay = findBDay();
         if (bDay >= 0) {
           document.dataForm.outputArea.value +=
             "\n" + ephem[bDay][0] + " " + ephem[bDay][1] + " " +
             ephem[bDay][2];
         } else {
           document.dataForm.outputArea.value +=
             "\n" + "Invalid entry";
         }
       }
     </script>
     </head>
     <body onLoad="document.dataForm.outputArea.value='';">
       <form name="dataForm" action="">
         Enter birthday (yyyy-m-d):
         <input type="text" name="yField" value="" size=4> -
         <input type="text" name="mField" value="" size=2> -
         <input type="text" name="dField" value="" size=2>
         <input type="button" value="  Find  "
           onclick="findBirthData()">
         <textarea name="outputArea" rows="16" cols="43"></textarea></td>
       </form>
     </body>
</html>

The problem is that unless I un-comment the alert in the findBDay 
function, the first time I click on the button nothing happens because 
ephem hasn't yet be loaded. When I click the button a second time it 
works OK.

I tried to start the loadFile() with <body onload="...> but it doesn't 
work. I've also tried to delay the execution of the for loop in the 
findBDay function while ephem.length is 0, but haven't managed to find a 
working solution. Only the alert works, but of course this is not a 
satisfactory solution.

Suggestions?

emf

-- 
It ain't THAT, babe! - A radical reinterpretation
https://files.nyu.edu/emf202/public/bd/itaintmebabe.html

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Compressing js file? emf <emfril@gmail.com> - 2013-12-23 14:31 -0500
  Re: Compressing js file? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-23 22:08 -0200
  Re: Compressing js file? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-12-23 16:40 -0800
    Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-24 13:23 -0500
    Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-26 21:56 -0500
      Re: Compressing js file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-27 09:52 +0100
        Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-27 08:18 -0500
          Re: Compressing js file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-27 16:59 +0100
            Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-27 15:32 -0500
              Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-28 15:12 -0500
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-01 02:17 -0500
        Re: Compressing js file? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-28 20:20 +0000
      XHR with timing issues (was: Compressing js file?) Christoph Michael Becker <cmbecker69@arcor.de> - 2013-12-27 22:33 +0100
        Re: XHR with timing issues (was: Compressing js file?) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-27 23:26 +0100
          Re: XHR with timing issues Christoph Michael Becker <cmbecker69@arcor.de> - 2013-12-28 12:08 +0100
            Re: XHR with timing issues emf <emfril@gmail.com> - 2013-12-28 09:04 -0500
  Re: Compressing js file? wolfgang zeidler <invalid@invalid.invalid> - 2013-12-27 22:33 +0100
    Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-28 00:16 -0500
      Re: Compressing js file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-28 11:13 +0100
        Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-28 11:26 -0500
          Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-28 13:53 -0500
            Re: Compressing js file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-28 21:00 +0100
              Re: Compressing js file? emf <emfril@gmail.com> - 2013-12-29 21:47 -0500
                Re: Compressing js file? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-12-31 14:05 -0800
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-01 02:50 -0500
                Re: Compressing js file? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-01-01 01:16 -0800
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-01 08:13 -0500
                Re: Compressing js file? Tim Streater <timstreater@greenbee.net> - 2014-01-01 14:17 +0000
                Re: Compressing js file? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-01-01 15:31 -0800
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-01 18:38 -0500
                Re: Compressing js file? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-01-01 15:45 -0800
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-02 11:43 -0500
                Re: Compressing js file? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-01-02 09:34 -0800
                OT: Compressing js file? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-02 19:52 +0100
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-03 07:19 -0500
                Re: Compressing js file? emf <emfril@gmail.com> - 2014-01-03 07:31 -0500
                Re: Compressing js file? Tim Streater <timstreater@greenbee.net> - 2014-01-02 09:14 +0000
                Re: Compressing js file? Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-01 16:25 +0000
                Re: Compressing js file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-01 21:36 +0100
          Re: Compressing js file? wolfgang zeidler <invalid@invalid.invalid> - 2013-12-29 20:06 +0100
            Re: Compressing js file? wolfgang zeidler <invalid@invalid.invalid> - 2013-12-29 20:27 +0100
        Re: Compressing js file? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-29 18:02 +0000
          Re: Compressing js file? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-12-30 18:17 +0100
            Re: Compressing js file? John Harris <niam@jghnorth.org.uk.invalid> - 2013-12-31 10:52 +0000
            Re: Compressing js file? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-12-31 13:38 +0000
              Re: Compressing js file? John Harris <niam@jghnorth.org.uk.invalid> - 2013-12-31 15:02 +0000
                Re: Compressing js file? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-01-01 16:42 +0000
                Re: Compressing js file? Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-01-01 21:32 +0000
            Re: Compressing js file? Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-12-31 21:44 +0000

csiph-web