Posts Tagged ‘javascript’

loop through json object

July 2nd, 2010

p = {k1: v1, k2, v2, k3, v3};
for (var key in p) {
if (p.hasOwnProperty(key)) { // make sure that the key you get is an actual property of an object, and doesn’t come from the prototype:

compact js

May 28th, 2010

Tool list:

Here are some tools I have found for compact js file in order to save the downloading time.

  • JSMin. It can remove comments and unnecessary whitespaces from js code. The source code (a C file) can be downloaded here. After compilation:

    gcc -o jsmin jsmin.c

    , you can use it like this:

    jsmin <global.js >global.min.js

  • jscompact. It promises to both compact and obfuscate javascript code. However, it depends on ‘Mozilla libjs’ (also called SpiderMonkey) which you can download the src and install it by yourself. NOTE: the newest version of libjs till now is 1.8.0-rc1 which, however is not compatible with the newest jscompact-1.1.1 that (I think) is based on libjs-1.6.0. And if you compile jscompact with js-1.8.0-rc1, you will get errors like this:

    jscompact.c:45: warning: format not a string literal and no format arguments
    jscompact.c: In function ‘shorten_function_vars’:
    jscompact.c:185: error: invalid type argument of ‘->’ (have ‘JSObject’)
    jscompact.c:190: error: ‘js_GetLocalVariable’ undeclared (first use in this function)
    jscompact.c:190: error: (Each undeclared identifier is reported only once
    jscompact.c:190: error: for each function it appears in.)
    jscompact.c:191: error: ‘js_GetArgument’ undeclared (first use in this function)

    OK, here are the right steps to build jscompact. (Remember to replace DIR_LIBJS & DIR_JSCOMPACT with your own dirs.)

    • download libjs-1.6.0 and extract it into DIR_LIBJS.
    • download jscompact and extract it into DIR_JSCOMPACT.
    • compile libjs. Enter DIR_LIBJS/src, compile it with:

      gmake BUILD_OPT=1 -f Makefile.ref

      . And it will build the targets into a new dir Linux_All_OPT.OBJ under src.

    • install the Make a soft link of into /usr/lib

      ln -s -LDIR_LIBJS/src/Linux_All_OPT.OBJ/ /usr/lib/

      which will be used by the final executable jscompact. Of course, you can also simply copy the Linux_All_OPT.OBJ/ into /usr/lib

    • compile jscompact. Enter DIR_JSCOMPACT, and build it:

      gcc -IDIR_LIBJS/src/Linux_All_OPT.OBJ -IDIR_LIBJS/src -g -DXP_UNIX -LDIR_LIBJS/src/Linux_All_OPT.OBJ -ljs -Wall -ojscompact jscompact.c

    Now you can use it like this:

    /jscompact –infile global.js –outfile global.min.js

  • Javascript Minimizer. It’s a .exe file and promises to remove insignificant to javascript characters (comments, tabs, newline, etc…) from *.js files. Haven’t tried yet.


JsMin will removes all the unnecessary white spaces but won’t shorten the function arguments and the local scope variables. JsCompact will remove most of the white spaces and shorten arguments and variables as well. However it leaves a lot of spaces which I think it’s not necessary. Given a 65,280B file, JsCompact reduces the size to 40,532B, while JsMin reduces it to 39,981B.

So I think a good choice is to combine the both. First compact with JsCompact, and remove the spaces with JsMin. With this method, the file 65,280B file can be finally reduced to 37,271B.