4 ms·
that's funny, I'm the other way around.. whenever I see syntactic sugar implemented via javascript, I unroll it in my head and consider it overhead, compared to
by thinkingisfun 14y ago
that's funny, I'm the other way around.. whenever I see syntactic sugar implemented via javascript, I unroll it in my head and consider it overhead, compared to sending a bunch of more bytes down the wire which hardly matter after gzip.
- thedz 14y agoTIL javascript functions are syntactic sugar.
- thinkingisfun 14y agoAnd TIL stands for what, exactly? Sure you would not simply equate "syntactic sugar" with "syntactic sugar implemented in javascript".. so what are "TIL javascrip functions"? I tried searching, but no luck, woth none of the permutations. There are too many acronyms for TIL to just blindly guess. My point basically was that _(listItems).invoke('remove') is actually short for * [content of http://underscorejs.org/underscore-min.js http://underscorejs.org/underscore-min.js] _(listItems).invoke('remove')* and even if you removed all non-called parts from underscore, that's still a lot more complex than for(var index = 0, length = listItems.length; index < length; index++) { listItems[index].remove(); } it's just hidden, out of sight and out of mind.
- Terretta 14y agoTIL stands for Today I Learned. They were attempting to use a meme acronym sarcastically -- an example of content-free commenting that works well for karma elsewhere but hopefully doesn't gain a foothold here. On topic, I agree with your amplification of your point.
- brianfryer 14y agoIf you use memes on HN, you're going to have a bad time. (They tend to be downvoted to oblivion, 'round these parts.)
- thinkingisfun 14y agoOh. Thanks, I get it now. I guess while syntactic sugar is usually considered a functionality of the language itself, it surely is used in the way I meant it, too... random search result: http://www.developerdrive.com/2012/05/an-introduction-to-underscore-js-–-part-2-array-like-collections/ http://www.developerdrive.com/2012/05/an-introduction-to-und... "In this part of the tutorial we’re going to take a look at some of the syntactic sugar that Underscore gives us to work with array-like collection." And of course, even if I had been utterly wrong for using the term, my point would stand unmoved, but you know that :)
- brown9-2 14y agoI've been using underscore for a few months and had no idea that you could call _ as a function until today. Pretty amazed that I missed this all along. Thanks for this!
- mikeryan 14y agoI can get where you're going with this, but as someone who uses underscore everyday I have to say in this particular case I don't agree with you. Underscore as a production library is 4kb. The functions are terse, and its extremely easy to understand what they're doing - nothing is generally hidden (this example is pretty easy to understand, yet one of the most "complex" uses of underscore I've seen). At the end of the day on a production javascript app of multiple thousand lines of JS, it likely actually saves me bytes over the wire from not having to write "for(var x = 0...." every time I want to iterate through something or filter an array or find if an array includes something etc (There's around 70 bytes removed in this single example).
- thinkingisfun 14y agoWell, of course it matters how often you do it. I would not do it in loops, or often called event handlers, such as scrolling. If you do it once on page load, sure, it hardly matters. But if you do it a lot in your code, that just means you can easily copy and paste it from a nearby place? At least that's what I often end up doing.
- tomjakubowski 14y agoUnderscore uses native methods for map/reduce/forEach when possible. For a modern browser the performance difference is comparable, or even faster for a map than an unrolled for loop: http://jsperf.com/native-for-loop-vs-array-foreach-and-array-map-vs-lodas http://jsperf.com/native-for-loop-vs-array-foreach-and-array...