case
22Jul/112

Pixel Programming

If you're old enough you may have seen the times when programmers wrote code as optimal as possible given the computers available back then. I remember 3D shooter games that would average 300 - 400 KB in size and you would play countless levels for days. Here's for example a 96 KB 3D shooter game called .kkriger that will leave you breathless.

full1 300x225 Pixel Programming

If you try now to explain people these days that it takes less space to write a 3 minutes HD animated video and music clip than than it takes to store a 32×32 pixel icon they won't believe you. Not now when a plain jpeg image used on some website is > 50KB. So here's a guy called Iñigo Quílez who demonstrates just that using the image analogy. He writes a x86 real mode demo using Photoshop. The entire program is done by creating a 9x9 pixels image, filling each pixels with carefully chosen colors to represent the correct opcodes (who needs compilers, right ?) The image is then saved in a .raw format (no headers) then renames it to a .com file so that Windows recognize it as a executable. The animation is a infinite moving tunnel with a gray colored texture created with an XOR pattern, blurred, and deformed using a plane deformation technique to produce the animation.

This is the "source code" resized 10 times in a png extension

pixel01 Pixel Programming

And here's the guy in action proving his mad skills.

1Jul/110

Read data directly from base64 without decoding

So here's the catch. You have a binary input you want to mess with in your browser for whatever reason. First problem you encounter is when you load the file. In IE you will have trouble reading a binary string because it uses null terminated strings, and your file will end at the first 0x00 byte found. If you manage to overcome that but trust me it is very CPU intensive. See the source of Read/Load files from ZIP in Javascript post. You will have to create a vbScript that will convert the received AJAX response into an array with decimal values for each byte in the file. Depending on your file size, it will take accordingly (10+ seconds * file KB). For every other browser it works just fine with plain strings.

Okay, one other potential issue may come if you need to receive the data into a JSON string or XML or whatever beside a plain binary file. Then you have to use BASE64 encoding and decode your data into a variable but must keep in mind the first issue with the null characters. My solution to this problem is to create a method to read directly from the BASE64 encoded string at arbitrary locations without decoding the entire thing.

Here's an example of a binary file:

Offset(h)00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
 
00000000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F     ...............
00000010 19 84 74 68 69 73 20 69 73 20 61 20 74 65 73 74     ..this is a test

You have 32 bytes in this file, with BASE64 encoding you get a 46 chars length string:

   AAECAwQFBgcICQoLDA0ODxmEdGhpcyBpcyBhIHRlc3Q=

A usage example:

    var data = new Base64Native("AAECAwQFBgcICQoLDA0ODxmEdGhpcyBpcyBhIHRlc3Q=");
    console.log(data.length); // outputs 32;
    console.log(data.readBoolean()); // outputs false; - reads a byte and casts it to a boolean value
    console.log(data.readByte()); // outputs 1;
    console.log(data.readWord()); // outputs 515; - reads a 16 bit number
    console.log(data.readInt()); // outputs 67438087; - reads a 32 bit number
    console.log(data.seek(28)); // moves the pointer to byte 28 (0x1C)
    console.log(data.readString(data.bytesAvailable())); // outputs 'test'; reads a string from position 28 to the end of the file

And here's the class

var Base64Native = function(data) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
        length = data.length - (data.length / 4) - (data.split(/=/g).length - 1),
        ptr = 0;
 
    function keyIdx(idx) {
        return keyStr.indexOf(data.charAt(idx));
    }
 
    function readByte() {
        if (!(length - ptr)) { throw Error('End of stream!'); }
        var blockIndex = Math.floor(ptr / 3) * 4, subIndex = ptr++ % 3, ptrChr = blockIndex + subIndex;
        return !subIndex && ((keyIdx(ptrChr) < < 2) | (keyIdx(ptrChr + 1) >> 4)) || subIndex === 1 &&
               (((keyIdx(ptrChr) & 15) < < 4) | (keyIdx(ptrChr + 1) >> 2)) || ((keyIdx(ptrChr) & 3)  < < 6) |  keyIdx(ptrChr + 1);
    }
 
    return {
        /* Returns the available bytes left in the file to read */
        bytesAvailable : function() {
            return length - ptr;
        },
        /* Reads and returns a 8 bit number and increments the position of the index accordingly */
        readByte : function() {
            return readByte();
        },
        /* Reads and returns a 32 bit number and increments the position of the index accordingly */
        readInt : function() {
            return (readByte() << 24) | (readByte() << 16) | (readByte() << 8) | readByte();
        },
        /* Reads 1 byte and returns the boolean value of it and increments the position of the index accordingly */
        readBoolean : function() {
            return Boolean(readByte());
        },
        /* Reads and returns a 16 bit number and increments the position of the index accordingly */
        readWord : function() {
            return readByte() << 8 | readByte()
        },
        /* Reads and returns a string of length 'len' and increments the position of the index accordingly */
        readString : function(/*String*/len) {
            var data = '';
            for (var i = 0; i < len; i++) {
                data += String.fromCharCode(readByte());
            }
            return data;
        },
        /* Moves the index into the file at location newpos */
        seek : function(newpos) {
            ptr = newpos;
        },
        /* Returns the position of the index in the file */
        position : function() {
            return ptr;
        },
        /* The length of the data */
        length : length
    };
};
4May/110

jQuery.quickEach

With jQuery, when you need to iterate all elements with a specific class for example, you use the each() method and inside the callback function you must convert the DOM element you receive as this to a jQuery object in order to access it's jQuery specific methods: $(this)
This is not such a big issue but if you have a lot of items and/or do this often, then times ads up. It would be a blast to have the this object directly as a jQuery instance. So here's a $ little plugin that does exactly that

Source website:
http://jsperf.com/jquery-each-vs-quickeach

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jQuery.fn.quickEach = (function() {
  var jq = jQuery([1]);
  return function(c) {
   var i = -1,
       el, len = this.length;
   try {
    while (++i < len && (el = jq[0] = this[i]) && c.call(jq, i, el) !== false);
   } catch (e) {
    delete jq[0];
    throw e;
   }
   delete jq[0];
   return this;
  };
 }());

If you run the test on that page, on most browsers you'll see that quickEach() is about 80% faster than native each()

30Mar/110

For Javascript devs

If you write Javascript apps you may want to dynamically add some HTML here and there and having HTML strings that need concatenation with different variables is bad and ugly at the same time. So here is a little nugget you might find useful.

1
2
3
4
5
    // standard style
    x = '<div class="' + classnames + '" rel="' + somevar + '" id="' + othervar + '">' + content + '</div>';
 
    // html tag style
    x = htmlTag('div', {class:classname, rel:somevar, id:othervar}, content);

Usage

htmlTag(tagName [, attributes [, content]])
or
htmlTag(tagName [, content [, attributes]])

The function can be called with one, two or three arguments. Beside the first one which is the name of the tag (string), the other two are optional and can be placed in whatever order you like.
For img tags, the content argument is set as src attribute or as value attribute for input tags if the attributes argument doesn't specify otherwise

attributes argument must be an object with key:value pairs and will be expanded as attributes of the tag
content argument is a string or number

1
2
3
4
5
6
7
8
9
10
11
12
13
    var classname = 'test', somevar = 2, id = 'mydiv', content = 'hello';
 
    htmlTag('div', {class:classname, rel:somevar, id:othervar}, content);
    htmlTag('div', content, {class:classname, rel:somevar, id:othervar});
    // will both return <div class='test' rel='2' id='mydiv'>hello</div>
 
    htmlTag('img', content)
    // <img src='hello' />
    htmlTag('img', {src:'myImage.jpg'}, content)
    // <img src='myImage.jpg' /> content is discarded and attributes src is used as source
 
    htmlTag('input', {type:'text', name:id}, content)
    // <input type="text" name="mydiv" value="hello" />

The code

1
2
3
4
5
6
7
8
9
10
11
function htmlTag(type) {
    var r="< "+type,o={},v='',x=0,a=arguments;
    a.length==3&&(((x=typeof a[1]=='object')||1)&&((o=a[-~!x]||{})&&(v=a[-~x]||'')))||a.length==2&&(typeof a[1]=='object'&&(o=a[1]||{})||(v=a[1]||''));
    for(var i in o)r+=" "+i+"='"+o[i]+"'";
    (type=='img'||type=='br'||type=='input')&&((!o.src&&v)&&((type=='img'&&(r+=" src='"+v+"' />"))||(type=='input'&&(r+=" value='"+v+"' />")))||(r+=' />'))||(r+=">"+v+"< "+"/"+type+">");
    return r;
}
 
function queryTag(type) {
     return $(htmlTag.apply(this, arguments))
}

queryTag function just wraps the newly created html in a jQuery object so you can have something like this

1
2
3
4
5
     $("#menu").append(
             queryTag('div', {class:'menuItem'}, 'Click me').click(function() {
                      alert('clicked');
             })
      )
1Oct/1049

Read/Load files from ZIP in JavaScript

At work we do a lot of JavaScript applications that have little to no HTML files (beside the index), everything is generated by JavaScript and we have tons of .js files that load at runtime and to reduce the size we have to minify them and that is really hard to debug and watch in the browser if something goes wrong. So I've made this JavaScript class that comes to help. What can you do is Zip up all your assets (js, images, css) and use ZipLoader class to read the zip and unpack your resources whenever you need them. It's small (10KB), easy to use and quite fast considering the extraction is made by JavaScript.

1
2
3
4
5
6
7
8
9
10
   // loads and caches the zip file
   var loader = new ZipLoader('files.zip');
   // creates a style node in the document header and appends the style
   loader.LoadCSS('files.zip://style.css');
   // creates a script node in the document header and appends the script
   loader.loadScript('files.zip://jquery.js');
   // returns the file content
   var someFileYouNeed = loader.load("files.zip://myFile.txt");
   // returns the base64 encoded image usable as img source
   $("#logo").attr('src', loader.loadImage('files.zip://images/logo.png'));

The level of compression using zip surpasses the js and css minification in all the ways possible. For example, My current application has 79 files (js, and few css) and they all sum up to 924KB in size. Zipping all those 96 files using Ultra compression level gives me 1 file of 126 KB => 1 server request. That's something.

The best performance is made Opera (strangely), then Firefox, Crome, Safari and last IE (no surprise here, I was expecting it before I even started).

It's a V 1.0 that still needs some work and it doesn't work in IE because that stupid browser overrides the mime type and at first null character in the reading of the zip, the content ends. I will fix this eventually icon smile Read/Load files from ZIP in JavaScript I will also include this as a jClass library soon.

Update: Fixed it so it works in IE as well. There is a performance issue still. Will fix that too.

Till then, HERE are the sources and a demo.

The zip in the demo is made using 7Zip with Ultra Compression level.

Happy codding.