Thursday, October 25, 2007

Random Strings

Today I created what is possibly the most efficient way to create a random string:
function randomString(length){
    var max = "";
    for (i=0;i<length; i++){
        max += "z";
    }
    return Math.round(Math.random() * parseInt(max, 36)).toString(36).toUpperCase();
}

So how, you ask, does it work? Well, it uses the fact that you can encode a number with any radix up to 36 which uses 0-9a-z. Perfect for creating a random string. SWe take the number of characters that we want and create our maximum encoded value (by repeating Z as often as necessary), and convert that into an integer. We then take that integer and use it as the upper bounds for a random number generator. We then convert that random number to a string using a radix of 36 and convert it to upper case for fun.

I'm not really thrilled about looping to create my max string but what can you do?

It would be possible to create strings without numbers by using a radix of 26 and mapping numbers to letters after the fact, but that's a lot of extra work.

Thursday, October 4, 2007

Things I'd like to see in JavaScript

Recently, someone asked me what features I'd like to see added to JavaScript. In the week or so since then I've had some time to think about it. Here are some of my thoughts:

Code inclusion

Right now, when we want to dynamically includes some javascript on the fly (for instance to ensure that we only load the features that we need) we have to resort to document.writing a script tag. This is exceptionally inefficient. I would be nicer if we could explicitly include javascript files using something like: includeScript('/scripts/myScript.js'). It might be nice to allow a call back function to be passed along too.

Site persistent JavaScript

Currently, every time you unload a page, the compiled javascript is tossed out the door and new javascript is compiled when the next page loads. I don't know about you, but 80% of my code doesn't change page by page. It would be great if we had a way to "save" our compiled JavaScript and simply reuse it on each page. This way, I can load my libraries once and never have to worry about it again. It would also be necessary to identify some code that lasts only for a single session and some that lasts "forever" (same idea as with persistent cookies)

Native set objects

In the past I've run into confusion with some of the new Array methods that are being introduced by Mozilla. Specifically, it turns out that [].any(function (){return true;}) !== [].all(function (){return true;});.

When I chatted with some of the Mozilla guys about this they said that this has to do with set math. That got me to thinking about sets in general. There are some things that one can do with sets that are generally useful.

  • You can intersect, union, difference sets. Calculating the intersection of two sets (lets say sets of DOM Nodes) can be exceptionally useful.
  • Every member of a set is unique. Again this is a useful feature for working with collection of DOM Nodes.

There are other features of sets that I would like to be able to use. I could build a JavaScript implementation of a set but its MUCH slower than a native implementation would be. Since its generally useful, its worth adding to the language

Date Literals

Since strict JSON only allows literal notation, there is currently no way to include real dates in a JSON construct. Dates are one of the most useful objects we have in JavaScript and having a literal for it would be wonderful.

Those are my ideas. What do you think would be a good addition?