Last active
June 20, 2021 08:51
-
-
Save tmcw/1c7dd591b11f0fa55cdc to your computer and use it in GitHub Desktop.
Revisions
-
tmcw revised this gist
Sep 18, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,7 @@ var result = array.map(function(item) { }); var result = array.map(function(memo, item) { }); ``` * When you are _mapping a list of things into one thing_. For instance, taking a sum or a count or some kind of aggregate figure based on a big list of things. ## `forEach` -
tmcw revised this gist
Sep 17, 2014 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,20 +2,37 @@ ## For loops ```js for (var i = 0; i < array.length; i++) { } ``` * Code has to run in old versions of IE. * Code is _extremely hot_: like, code that runs for tens of thousands of iterations. * When you want to use old-fashioned control statements like `break` or `continue` or `goto` to control your flow ## `.map` ```js var result = array.map(function(item) { }); ``` * When you are _mapping one kind of things to another kind of thing_. The `.map` iterator is all about the fact that _what you return from the function gets added to an array_, and you should want that. Otherwise you're creating a big new array for no reason that's just filled with `undefined`. ## `.reduce` ```js var result = array.map(function(memo, item) { }); ``` * When you care _mapping a list of things into one thing_. For instance, taking a sum or a count or some kind of aggregate figure based on a big list of things. ## `forEach` ```js array.forEach(function(item) { }); ``` * When you aren't mapping things to another kind of thing, or reducing things to one thing, but doing something else. * When you want a scope per iteration: for instance, if you're doing async things and want your variables to not change values every iteration. -
tmcw created this gist
Sep 12, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ # What kind of iteration to use when in JavaScript ## For loops * Code has to run in old versions of IE. * Code is _extremely hot_: like, code that runs for tens of thousands of iterations. * When you want to use old-fashioned control statements like `break` or `continue` or `goto` to control your flow ## `.map` * When you are _mapping one kind of things to another kind of thing_. The `.map` iterator is all about the fact that _what you return from the function gets added to an array_, and you should want that. Otherwise you're creating a big new array for no reason that's just filled with `undefined`. ## `.reduce` * When you care _mapping a list of things into one thing_. For instance, taking a sum or a count or some kind of aggregate figure based on a big list of things. ## `forEach` * When you aren't mapping things to another kind of thing, or reducing things to one thing, but doing something else. * When you want a scope per iteration: for instance, if you're doing async things and want your variables to not change values every iteration. # TL;DR `.map` and `.reduce` are functions that make it easier to transform data. You can use them as general-purpose "I want a loop" functions but it's not semantic and can be wasteful: if you just need a loop, use `.forEach` or `for`. --- FAQ: * why use for loops for hot code? Well, functional iterators need to create and dispose of a variable scope every time they run. This makes them slightly slower.