Created
July 29, 2025 03:00
-
-
Save mokshchadha/81e4f49374bfc37521d14a4b5a9e6625 to your computer and use it in GitHub Desktop.
List of examples on how you can use and benchmark JS iterators
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
// ECMAScript 2025 Iterator Examples | |
// New built-in Iterator global with functional operators | |
// Example 1: Working with Arrays using Iterator helpers | |
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
const result = Iterator.from(numbers) | |
.filter(x => x % 2 === 0) // Keep even numbers | |
.map(x => x * x) // Square them | |
.take(3) // Take first 3 | |
.toArray(); // Convert back to array | |
console.log(result); // [4, 16, 36] | |
// Example 2: Working with Generator Functions | |
function* fibonacci() { | |
let a = 0, b = 1; | |
while (true) { | |
yield a; | |
[a, b] = [b, a + b]; | |
} | |
} | |
const fibNumbers = Iterator.from(fibonacci()) | |
.take(10) // Take first 10 fibonacci numbers | |
.filter(x => x > 5) // Filter numbers greater than 5 | |
.map(x => `Fib: ${x}`) // Format them | |
.toArray(); | |
console.log(fibNumbers); // ["Fib: 8", "Fib: 13", "Fib: 21", "Fib: 34"] | |
// Example 3: Lazy Evaluation - Processing Large Datasets | |
function* generateLargeDataset() { | |
for (let i = 1; i <= 1000000; i++) { | |
yield { id: i, value: Math.random() * 100 }; | |
} | |
} | |
// This is memory efficient - no intermediate arrays created | |
const processedData = Iterator.from(generateLargeDataset()) | |
.filter(item => item.value > 50) | |
.map(item => ({ ...item, processed: true })) | |
.take(5) | |
.toArray(); | |
console.log(processedData); | |
// Example 4: Working with Strings | |
const text = "Hello World JavaScript"; | |
const processed = Iterator.from(text) | |
.filter(char => char !== ' ') | |
.map(char => char.toUpperCase()) | |
.take(10) | |
.toArray() | |
.join(''); | |
console.log(processed); // "HELLOWORLD" | |
// Example 5: Chaining Multiple Operations | |
const users = [ | |
{ name: "Alice", age: 25, active: true }, | |
{ name: "Bob", age: 30, active: false }, | |
{ name: "Charlie", age: 35, active: true }, | |
{ name: "Diana", age: 28, active: true }, | |
{ name: "Eve", age: 32, active: false } | |
]; | |
const activeUserNames = Iterator.from(users) | |
.filter(user => user.active) | |
.filter(user => user.age > 25) | |
.map(user => user.name.toUpperCase()) | |
.toArray(); | |
console.log(activeUserNames); // ["CHARLIE", "DIANA"] | |
// Example 6: Drop and Take for Pagination-like Behavior | |
const items = Array.from({ length: 100 }, (_, i) => `Item ${i + 1}`); | |
const page2Items = Iterator.from(items) | |
.drop(10) // Skip first 10 items (page 1) | |
.take(10) // Take next 10 items (page 2) | |
.toArray(); | |
console.log(page2Items); // ["Item 11", "Item 12", ..., "Item 20"] | |
// Example 7: Performance Comparison | |
console.time('Array methods'); | |
const arrayResult = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
.filter(x => x % 2 === 0) | |
.map(x => x * x) | |
.slice(0, 3); | |
console.timeEnd('Array methods'); | |
console.time('Iterator methods'); | |
const iteratorResult = Iterator.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | |
.filter(x => x % 2 === 0) | |
.map(x => x * x) | |
.take(3) | |
.toArray(); | |
console.timeEnd('Iterator methods'); | |
// Both produce the same result, but Iterator methods are more memory efficient | |
console.log('Array result:', arrayResult); | |
console.log('Iterator result:', iteratorResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment