Results 1 to 11 of 11

Thread: Compress javascript source ?

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Compress javascript source ?

    I have a particularly long javascript, and was wondering if its possible to compress it down, and have it extract itself at the client ?

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    why is it a problem?

    if it makes the source too long, just make it an external file.

    here's what you can try doing though:
    delete all comments
    rename variables to something shorter
    rename functions to something shorter
    get rid of whitespace
    if you have many short lines like this:
    a=1
    b=2
    etc.
    change it to:
    a=1;b=2 etc.
    can't think of anything else
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Its actually just a very long list of products and manufacturers.
    I already have this done in ASP.Net, but now I'm adding some intelligence to a web-form, and allowing for some client-side processing to allow the client to produce more complex searches.

    This is what the file looks like :

    Code:
    var strMasterHeaders = new Array(26 );
    strMasterHeaders[0] = '1_Backup Solutions';
    strMasterHeaders[1] = '2_Boards (video, sound, .)';
    strMasterHeaders[2] = '3_CAD Peripherals';
    strMasterHeaders[3] = '4_CAD Software';
    strMasterHeaders[4] = '5_Connectivity (hub, switch, router,.)';
    ... etc
    
    var strSecondHeaders = new Array(100 );
    strSecondHeaders[0] = '37_14 -Inch Monitor';
    strSecondHeaders[1] = '38_15 -Inch Monitor';
    strSecondHeaders[2] = '39_17 -Inch Monitor';
    strSecondHeaders[3] = '40_19 -Inch Monitor';
    strSecondHeaders[4] = '41_20 -Inch Monitor';
    strSecondHeaders[5] = '42_21 -Inch Monitor';
    strSecondHeaders[6] = '10_Architecture & Design';
    strSecondHeaders[7] = '1_Backup Software';
    strSecondHeaders[8] = '12_Bridges & Routers';
    strSecondHeaders[9] = '95_Bulk Storage Devices';
    strSecondHeaders[10] = '11_CAD Retail';
    strSecondHeaders[11] = '3_Card Accessories';
    ... etc
    
    var strManufacturers = new Array(82 );
    strManufacturers[0] = '3Com';
    strManufacturers[1] = 'ACCO brands';
    strManufacturers[2] = 'Acer';
    strManufacturers[3] = 'Adaptec';
    strManufacturers[4] = 'Adobe';
    strManufacturers[5] = 'Allied Telesyn';
    strManufacturers[6] = 'Antec';
    strManufacturers[7] = 'AOC';
    strManufacturers[8] = 'AOpen';
    ... etc

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    all the semi colons at the end of the lines can go if you're not planning on combining lines.

    those variable names can definately be shortened. most people prefer long names (including me) but if you're desperate to make it shorter, that's one option.

    also
    a=new Array()
    a[0]="hello"
    a[1]="bye"
    can be shortened to:
    a=new Array("hello","bye")

    I'de leave it and shove it in an external file though.
    Have I helped you? Please Rate my posts.

  5. #5

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    hmmmm. I think I'll find a compressor.
    Compress the data and then base64 encode it. On Load, I'll unencode, and uncompress.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Base64 adds one third to the size, though.

    No, what you should do is set up your web server to support transfer compression, if the client does too it can use it. This is handled transparently and you don't need to worry.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by CornedBee
    Base64 adds one third to the size, though.

    No, what you should do is set up your web server to support transfer compression, if the client does too it can use it. This is handled transparently and you don't need to worry.
    Good idea. Do you know if IIS supports compression 'out-of-the-box' ?

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Don't know anything about the Ivil Information Server
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by CornedBee
    Don't know anything about the Ivil Information Server
    Watch your tongue peasant

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Another option (probably too labour intensive to bother with!) is to replace duplicated data with constants, eg:
    Code:
    var IM = ' -Inch Monitor';
    strSecondHeaders[0] = '37_14'+IM;
    strSecondHeaders[1] = '38_15'+IM;
    strSecondHeaders[2] = '39_17'+IM;
    strSecondHeaders[3] = '40_19'+IM;
    strSecondHeaders[4] = '41_20'+IM;
    strSecondHeaders[5] = '42_21'+IM;

  11. #11

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by si_the_geek
    Another option (probably too labour intensive to bother with!) is to replace duplicated data with constants, eg:
    Code:
    var IM = ' -Inch Monitor';
    strSecondHeaders[0] = '37_14'+IM;
    strSecondHeaders[1] = '38_15'+IM;
    strSecondHeaders[2] = '39_17'+IM;
    strSecondHeaders[3] = '40_19'+IM;
    strSecondHeaders[4] = '41_20'+IM;
    strSecondHeaders[5] = '42_21'+IM;
    I've already done that actually - well, the mostly duplicated items - i.e. manufacturer names are replaced with an index number...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width