September 2017
Intermediate to advanced
216 pages
6h 8m
English
The challenge with the data event is that the chunk of data that it gives to our event handler isn't something that's parsable. For example, it could leave off half of a word. This is no big deal—we'll just have to account for this:
let myWordList = List();let last = '';console.time('elapsed');const wordInput = fs.createReadStream('./input/words');wordInput.on('data', (data) => { const words = (last + data.toString()).split(os.EOL); last = words[words.length - 1]; myWordList = myWordList.concat(words.slice(0, words.length - 1));});wordInput.on('end', () => { console.log('word count', myWordList.count().toLocaleString()); console.timeEnd('elapsed'); // -> word count 235,886 // -> elapsed: 2012.116ms});
The data that's passed ...
Read now
Unlock full access