Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any point in creating a custom iterator for an array in node.js?

I need to parse an 80GB+ CSV file, and thought this a good opportunity to understand iterators in JavaScript (and then probably use an existing library such as csv-iterator, fast-csv, etc).

Looking at an iterator example on MDN HERE, I see this code:

function makeIterator(array) {
    var nextIndex = 0;

    return {
       next: function() {
           return nextIndex < array.length ?
               {value: array[nextIndex++], done: false} :
               {done: true};
       }
    };
}

Which is pretty self-explanatory. I can create an iterator for an array:

var iteratorForMyArray = makeIterator([1,2,3,4,5,6,7,8,9, etc])

And then I can use my shiny new iterator to 'iteratively' pull out values of my array:

var val0 = iteratorForMyArray.next().value
var val1 = iteratorForMyArray.next().value
etc

I can see why this is useful when parsing a CSV file. I'm wondering if there is any point in creating such an iterator for a simple array?

I often find that while simplified examples are useful for understanding, it's sometimes hard to see when the example is only useful as an example vs is actually useful in programming.

Because JavaScript already provides 2 mechanisms for creating iterators over array structures:

1: Basic for loop

for (let i = 0, i < [1,2,3,4,etc]; i++) {
  ...
}

2: Array.prototype.forEach

[1,2,3,4,etc].forEach(function(val, i, arr) {
  ...
})

(which I have just seen is slow on this site)

Questions:

  1. Does my custom iterator offer anything that these iterators don't?
  2. Do both these 'in-house' iterators create 'iterators' (i.e. what I understand as sequential pointers to values in data structures)? Or am I off by miles...
like image 423
Zach Smith Avatar asked Oct 18 '22 09:10

Zach Smith


1 Answers

ForEach guarantees order, as well as it'll skip invalid indexes etc. Your iterator doesn't provide anything extra when compared to a standard for loop, it'll just be slower, as you're calling a function to get the next item, where a basic for loop doesn't have this overhead. Use a for loop and cache the length to start for better performance.

like image 162
clint Avatar answered Nov 02 '22 04:11

clint