paigen11/file-read-challenge

Unnecessary loops in the close event

Closed this issue · 2 comments

First of all, thank you for your post, it was quite good.

For streaming, in every rl.on('line', call you already have the current line, and you are extracting firstNames and formattedTimestamp already.

But rl.on('close' call you are again running loop over firstNames dateDonationCount arrays which is unnecessary.

On your rl.on('line' event you can calculate those values and then you can remove the unnecessary loops in the close event. That way your measurement will be way lower than the previous one.

Just check this out.

readFileStream.js

if (firstHalfOfName.includes(' ') && firstHalfOfName !== ' ') {
	firstName = firstHalfOfName.split(' ')[0];
	firstName.trim();
	firstNames.push(firstName);      
        // make your calculation here, no need to loop over firstNames again
	dupeNames[firstName] = (dupeNames[firstName] || 0) + 1;
} else {
	firstNames.push(firstHalfOfName);
	dupeNames[firstHalfOfName] = (dupeNames[firstHalfOfName] || 0) + 1;
}

[....]

dateDonationCount.push(formattedTimestamp);
// here, eliminate the dateDonationCount.forEach loop in the close event
dateDonations[formattedTimestamp] = (dateDonations[formattedTimestamp] || 0) + 1;

Thanks for pointing that out! I took your suggestions and applied them, and you're totally right, it is quicker! Thanks again, I appreciate your taking the time to share that improvement. I've pushed the new and improved code to the repo now. 😃