Skip to content

Instantly share code, notes, and snippets.

@prajapati-parth
Created September 26, 2017 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prajapati-parth/d1567f1a796b1e46b32b9b401f8377b4 to your computer and use it in GitHub Desktop.
Save prajapati-parth/d1567f1a796b1e46b32b9b401f8377b4 to your computer and use it in GitHub Desktop.
A simple for loop with caching
let items = [
//unknown number of items
]
// normal for loop
// here items.length is calculated each time the loop cycles
for (let i=0; i<items.length; i++) {
// inside for
}
// for loop with caching
// here iterations is calculated once and the same is used for each cycle
for (let i=0, iterations=items.length; i<iterations; i++) {
// inside for
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment