Last active
January 19, 2017 14:45
-
-
Save gr0uch/b70963f6b9071bc2ccc1bd65c63a806b to your computer and use it in GitHub Desktop.
This file contains 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 characters
// Don't do functional programming in JS. It's wasteful. | |
var shoppingCart = [ { price: 12.99 }, { price: 5.99 }, { price: 3.10 } ] | |
// Loops twice and does two function calls per iteration. | |
var fpSum = shoppingCart | |
.map(function (product) { return product.price }) | |
.reduce(function (accumulator, price) { return accumulator + price }, 0) | |
// One loop, no unnecessary function calls. | |
var i, j, impSum = 0 | |
for (i = 0, j = shoppingCart.length; i < j; i++) | |
impSum += shoppingCart[i].price |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment