Last Updated: September 27, 2021
· mohmedsalem

Reading a 2GB text file ( php vs nodejs ) - comparison with a story

There are 2 heroes when it comes to a server side languages php & nodejs, of course there are many others but i'm talking about them specifically because i had an experience to learn and work with them, i was a fan of php i have done a lot of projects with it but i heard about another thing beautiful and powerful called nodejs and i was curios about learning it especially when i read that's based on javascript language or other simple definition of it is how to build your site using javascript, i was amused with the idea of working with one language in your project ( front-end & back-end ), and i started to compare the things the two of them do and the results was extremely close, actually the two languages did the same thing but when it comes to the speed nodejs is like a Ferrari in processing your code .
So i started a new project but i was confused which language should i use in my site, i started collecting the things i need and try it on each language, but i got stuck in a thing that php couldn't processed for me, the thing is reading a 2 GB text file that massive file of a database the site needs, php kept saying this nasty sentence ( Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted ) whenever i tried to read this file, i tried to alter the memory size and make it bigger to fit the file size but all i got in the end is more waiting for the page to load and in the end the browser crashed and my spirit for this site is gone.
After while i told my self why you try reading it with nodejs i was sure that nodejs wouldn't do the job that php didn't do because of the reputation the php have and also the number of years in the field especially that nodejs is new ( released in 2009 ) i found it worth the shot really and i tried the ( fs.createFileStream ) function that split the data into chunks and every chunk has a specific size that can hold then it wouldn't be a problem to nodejs to read a file like that because it split it into pieces and stream it so you can receive the data once that the data callback function is on, on the other hand php doesn't have the ability to do that kinda of thing ( php read the whole file and return the data not like nodejs can stream the data chunk by chunk ) i really found it a powerful advantage to nodejs that ability to split the data that sent with the response and processing it faster and the options that nodejs provide to modify the chunk size and choose the encoding yourself, i really found it interesting.
From my perspective the future is nodejs with its new power to the web world, developers should move to it now, i don't trying to say stop learning php or working without it but all i need to say from my experience that every thing develops and php still in the dust so you need to find another thing that fulfill your work in order to be in the top.
In case you have a 2GB text file you need to read with nodejs try this code :D :

2 Responses
Add your response

I guess you didn't use fopen in php which reads data in chunks. So you are totally misleading when you say "php read the whole file and return the data not like nodejs can stream the data chunk by chunk". It is because you choose wrong function.

PS: I am big fan and developer of node with little bit PHP background.

over 1 year ago ·

@okappss i tried fopen and it didn't work :D

over 1 year ago ·