My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Paweł Dąbrowski

2.1K reads

3 comments

Ruslan Hassonov
Ruslan Hassonov
May 16, 2023

You're suggesting to use .foreach instead of .each for parsing large CSV files. Could you elaborate why this is better?

·
·2 replies
Paweł Dąbrowski
Paweł Dąbrowski
Author
·May 16, 2023

Ruslan Hassonov the approach with .each will load all records into memory and then allow you to access them - this can crash your application if the file is big. From the other side, .foreach goes through every line of the file without loading it into memory at once. It's similar to find_each method in Rails when you deal with many records.

1
·
Ruslan Hassonov
Ruslan Hassonov
May 16, 2023

Paweł Dąbrowski ah, being a Rails developer, I was only familiar with .find_each. Thanks!

1
·