Category: Javascript

Why I have decided to quit using Cookie Jar

Recently I've been developing some JS stuff using Cookie Jar (source). Everything went great and Cookie Jar got along even with IE :) However, cookie jar cookie happened to grow and grow to fast.

Cookie Jar stores parts of Cookie as a key-value pair [key][value], so if you have a lot of small stuff - the cookie will be two times bigger than the data stored in it. Example:

[m][5] - number of chars: 2 (m,5); total amount: 6 ; waste 4 chars.

Phi, nothing special you say. Only 4 characters. In such a simple example maybe but if you store a lot of stuff, waste will be much bigger.

There has been additional problem. Some characters where stored as HTML entities. I don't know why - I've stored in cookie jar only numbers ;)

So, my cookie grew and grew more rapidly. And along with this - something else happened. Performance started to be really bad. Everything worked really slow. Can't say whether or not it is only Cookie Jar fault, but after switching to my own cookie engine, performance got better.

When I've been using Cookie Jar - "setting up" GUI took some time, so users felt this delay. Currently all init stuff goes really fast and none of them feels this.

Whether or not use Cookie Jar?

Cookie jar is convenient and easy to use. If you don't store whole bunch of small stuff in it and you don't care so much about performance - definitely you should use it. However if you need something really fast and thin - you should write your own dedicated cookie handler.

Using namespaces (modules) in Javascript

Recently I've been developing my own GUI engine in Javascript.

Nothing fancy, however it is quite big so I've decided to use namespaces (modules).

After reading some tutorials I've found Gravatar comment here.

I've really liked his method so I've decided to use it in my own projects. If you want to use namespaces, place this piece of code before any other JS stuff:

String.prototype.namespace = function(source) {
    Object.extend(this.split('.').inject(window, function(parent, child) {
        return (parent[child] = parent[child] || { });
    }), source || { });
}

Now You can use namespaces like this:

'Space.Subspace.Methods'.namespace({
    method: function(){
        alert('h hello world!');
    },
    method2: function(){
        alert('bye bye worlds!')
    }
});

After defining module, using its method is quite easy:

    Space.Subspace.Methods.method();
    Space.Subspace.Methods.method2();

There's more! Creating classes inside modules. First our base class (Class.create is a part of Prototype library):

var Parent = Class.create({
    class_method: function(){
        alert('im here!')
    }
});

Inheritance and namespace:

'Space.Subspace.Klasses'.namespace({
    SuperClass: Class.create(Parent, {})
});

I nie pozostaje nam nic innego jak utworzyć instancję klasy i sprawdzić czy działa:

    a = new Space.Subspace.Klasses.SuperClass();
    a.class_method();

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑