Lambda vs. function

Peter Michaux petermichaux at
Tue Oct 28 12:46:33 PDT 2008

On Mon, Oct 27, 2008 at 2:34 PM, Breton Slivka <zen at> wrote:
> I don't know if anyone will find this relevant or useful, but in my
> personal code, I have changed over to completely avoiding the switch
> statement altogether, in favor of primarily using objects with
> subscript notation.
> instead of
> switch(type) {
>   case "big":
>       //do something big
>     break;
>   case "small"
>       //do something small
>      break;
> }
> I would instead
> var cases = {
>   big: function (arg) { /* do something big */ },
>   small: function (arg) { /* do something small */ }
> }
> var result = (cases[type] ? cases[type](something):"default result";
> or, if each case has a lot of repeating code, I simply make an object
> factoring out the parameters that are different, and use the case as a
> look up for parameters, and feed them into the boiler plate code once.
> The result can be a little less readable unfortunately, but I find
> it's a bit tidier and succinct- and can also be much more readable if
> I manage to do it right. Plus, it completely avoids the fall through
> problem by making it impossible. Tail position might be a bit more
> clear with this approach as well.

I do roughly the same thing. Using an object is faster than "switch"
also, as far as I recall doing some tests. The speed of switch seems
to be the same speed as if-else if-else if-...-else.

I don't think I've ever used "switch" in JavaScript. I do like the
semantics of "cond" in Scheme.


More information about the Es-discuss mailing list