Created
July 12, 2022 16:25
-
-
Save ashishkpoudel/772a930a991dda5e9b18ea9dbe35202d to your computer and use it in GitHub Desktop.
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 characters
| 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