Memory Management & Garbage Collection

Which approach is more memory-efficient for processing a very large text string?
// Approach 1: Creating substrings
function processLargeText(text) {
  for (let i = 0; i < text.length; i++) {
    const substr = text.substring(i, i + 10);
    // Process substr...
  }
}

// Approach 2: Character-by-character access
function processLargeText2(text) {
  for (let i = 0; i < text.length; i++) {
    // Process 10 characters without creating substrings
    for (let j = 0; j < 10 && i + j < text.length; j++) {
      const char = text[i + j];
      // Process char...
    }
  }
}
Next Question (38/40)