'let' best practices

I don’t know why you would think that newModel is computed twice. Is it because you’re thinking that values are lazy like in Haskell?

Anyway, no, newModel is only computed once. FYI, this is roughly what your code compiles to in JavaScript:

function update(msg, model) {
	var newModel = complicatedTransformationOf(model);
	var cmd = someFunctionOf(newModel);
	return _Utils_Tuple2(newModel, cmd);
});

newModel is only computed once, and then it’s value is referenced/used in two places.

Your second version is pretty much the same. If anything, it might be a tad slower because you have more function calls, but it’s to a negligible degree (unnoticeable if your function isn’t computed thousands of times per second).

I don’t think you generally need to concern yourself with performance around let expressions too much. They don’t have specific performance problems.

Your first version is just fine: it’s readable and you don’t have any performance penalty in there. Keep it the way it is.

3 Likes