Case study #1

This article will be the first of a long serie I hope.
One of the purpose of this blog is education through example. Each time I find a good example of a problem solved in a way that I think I could improve, I will provide the code, examine it and show how I would the problem and justifying why I think it is better.

I encourage you to debate the open questions I could raise and I encourage you to submit me some code you find interesting.

This first case study comes from a code I have found in a website (variable names and strings have been changed).

var v = new (function () {
    this.a = "Hello ";
    this.b = "world !";
/* ...
** More than 1250 assignments like this ! 
** ... */
    this.y = "foo";
    this.z = "bar";
})();

Reading the code

A variable receives something. This something seems to be a new instance of the result of a function called once. It is not.
It turns out that when you remove the final (), the result is exactly the same. When you replace the final )() (take the value of what is between parenthesis and call it if it’s a function) by ()) (call the function and return the value returned by the function), Opera and Firefox explain that the result of the anonymous function call is not a constructor. Indeed, nothing is returned ! Not even this. In this case, the default returned value is undefined.

Finally, this code means that a constructor is called. This constructor is the anonymous function between parenthesis. The final () is interpreted as an empty (and useless) expression.

What problem are we solving, by the way ?

This code is called once at the beginning. It aims at initializing an object.

To do this I would just have created an object in the “JSON way” :

var v = {
    a : "Hello ",
    b : "world !",
/* ... */
    y : "foo",
    z : "bar"
};

My guess is that because I just have a unique assignment, my initialization should be faster than the 1200+ instructions of the previous constructor. Another advantage of my solution is that I got rid of the 1200+ “this.” which saves 6000+ characters !

Experiment

After putting my idea and the original one in two separate loops and comparing the times of 100, 1000 or 2000 run, my implementation turns out to be between 2 and 3 times faster. This “huge” range may be due to the high usage of strings and consequently the garbage collector which is known to be actively used on Firefox (where I ran my experiments) and whose behavior cannot be accurately predicted.

Note : I have not rewritten 1200 lines of code manually. I replaced all “;” by “,” and got rid of all “this.”. Some errors occurred due to the fact that some “this.” were in the right-hand side. Those must be kept. They refer to the value of the property of the object in construction.

Conclusion

Smaller code, no useless code, faster code. My solution seems to be better in all aspects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s