Skip to content

Instantly share code, notes, and snippets.

@gr0uch
Last active January 19, 2017 14:45
Show Gist options
  • Save gr0uch/b70963f6b9071bc2ccc1bd65c63a806b to your computer and use it in GitHub Desktop.
Save gr0uch/b70963f6b9071bc2ccc1bd65c63a806b to your computer and use it in GitHub Desktop.
// 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