4 ms·
Okay, so we're supposed to avoid function allocations like the plague, and using bitfields to represent sets of data. They 80's called, they want their optimiza
by qwertyuiop924 10y ago
Okay, so we're supposed to avoid function allocations like the plague, and using bitfields to represent sets of data. They 80's called, they want their optimizations back.
Using bitfields, in particular, is a heavy sacrifice in clarity for relatively few wins.
As for function allocations, internal functions can be hidden by binding the actual function to the return value of a closure that instantiates both the main and internal functions, and returns the main. You can't get rid of the cost of functions in the general case, though: a function's lexical environment has to be recreated each time the function is called (otherwise, all the variables are what C calls "static"), and JS's object semantics mean you can't just make one function, and rebind it to the appropriate parent lexical environment at call time (I have no idea if that's a useable optimization, but it occurs as a way to cut down on allocations) although you could bind the function code to multiple function objects with different parent lexical environments, which would largely accomplish the same thing (but now I'm just thinking out loud).
If you really need these optimizations, go ahead, use them, but don't take these tips unreservedly, and use them in your next project.
- userbinator 10y agoUsing bitfields, in particular, is a heavy sacrifice in clarity for relatively few wins. Only to those who don't understand bitfields, which IMHO means they don't even understand the basics of how a computer works, let alone how to effectively program one. JS is a high level language, but it still has bitwise operators for a reason.
- albinofrenchy 10y agoI disagree. Bitfields of somewhat unrelated fields is a sacrifice in clarity even if you understand bitfields perfectly well -- I'd argue anything requiring boilerplate sacrifices something in the way of being able to understand a code base.
- qwertyuiop924 10y agoI disagree. Bitfields mean that you have to do a lot of boilerplate operations which obscure the actual meaning of what you're doing, and can confuse even those who know what's going on, as an error can creep in easily.