Skip to content

Instantly share code, notes, and snippets.

@ashishkpoudel
Created July 12, 2022 16:25
Show Gist options
  • Select an option

  • Save ashishkpoudel/772a930a991dda5e9b18ea9dbe35202d to your computer and use it in GitHub Desktop.

Select an option

Save ashishkpoudel/772a930a991dda5e9b18ea9dbe35202d to your computer and use it in GitHub Desktop.
const withGenerator = () => {
function *largeDataSource() {
let counter = 0;
while (counter >= 0) {
counter++;
yield counter;
}
}
for (const item of largeDataSource()) {
console.log(item);
}
}
const withoutGenerator = () => {
// Runs out of memory
function largeDataSource() {
let [counter, store] = [0, []];
while (counter >= 0) {
counter++;
store.push(counter);
}
return store;
}
for (const item of largeDataSource()) {
console.log(item);
}
}
// console.log(withGenerator());
// console.log(withoutGenerator());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment