4 ms·
A neat collection of tricks, but I'm not sure I would ever actually employ any of them (except perhaps storing the length -- although I'd like to check that it
by jsankey 16y ago
A neat collection of tricks, but I'm not sure I would ever actually employ any of them (except perhaps storing the length -- although I'd like to check that it makes a difference first). There just doesn't seem to be a major advantage to any of the tricks, and they all come with some cognitive cost to the reader (and in some cases greater costs, e.g. failure on arrays with falsey values).
- grayrest 16y ago> except perhaps storing the length -- although I'd like to check that it makes a difference first) It makes a difference in the IEs. I don't remember it making much of a difference one way or the other with the JITted engines, though if the ES harmony proxies become widespread, there's a reasonable chance it'll become best practice again.
- zbanks 16y agoAnother trick is to do the loop backwards: for(var i = arr.length - 1; i >= 0; i--){...} I remember seeing a test that noted a visible difference between the two. Its arguable, however, if it makes a significant difference in the grand scheme of things...