Hi guys, Long story short, I read a file with a file reading API then sliced readable data into chunks and send them to reassemble by another function to avoid reading large amount of data. Part of the code would look like this: var data = ''; request.on('data', (chunk) => { data += chunk; }).on('end', () => { // do something with the data }); Code (markup): Then I found that someone does it like this: var data = []; request.on('data', (chunk) => { data.push(chunk); }).on('end', () => { // do something with the data }); Code (markup): So which method is better and should use. Thank you,
I rarely use the push method, and I tend to assign a value to an array element directly. You've declared your array correctly. var some_array = []; some_array[0] = x; Code (JavaScript): Using the new Array() constructor is ridiculous. You're looking for problems if you go with that. In terms of the code, what's your priority? Are you looking for efficiency, easy-to-read, backwards compatible? In terms of readability and straight-forward simplicity, I would choose to go with the first option.
Thanks for reply, My priority is always performance above all. The data reading should be something like youtube video. You grab data from file and send the very first chunk right away along the uphill reading road. Chunk data should be read and rendered as soon as it reached the assembly-factory function even the reading is not finished yet. Forget about backwards compatible, I'm not like most folks on this forum. I don't go backwards compatible and always stay with the cutting edge technology. If it only worked on Chrome, I go Chrome-only. That's what I do for a living. What's the benefit of it? Is it executed faster?
That would hinge entirely on what you're doing with that data. One's an array, one's a string... so how are you processing that data and why. What you're doing with it is the determining factor here, and you've not said that. Though both are the type of vague arrow function crap and mishandling of data handling that makes me HATE node.js and how people misuse it; in a "code clarity, who the **** needs that" kind of way.
I know what you mean about backwards compatibility. It's the same w/ cross-compatibility -- I find myself having to limit useful features. Now that HTML5 exists, I just write for that standard, and if a web client can't follow the HTML5 standards, why cater to it? Before HTML5 - it was such a f-ing mess. I ended up walking away. I honestly don't know. It comes across to me that using an assignment operation instead of invoking a method or function may be more efficient, but I guess I'd have to look this sort of thing up. I'm scared of node.js I don't think I've ever really needed to use it.
Thanks for the answer. I still have much to learn about this. I mostly use PHP an node.js to connect to the client's ERP system and make data viewable on the web browser and perform certain tasks. It's hardly a real web. Most users are corporate staffs. They do not tolerate time wasting and they want first-rate experience. So if you told them that the portal is accessible only by using Microsoft Edge and they just use Microsoft Edge. That's why I don't do much of things deathshadow suggested. It's in different environment. I like him contributing to the community though. I've built some few webs. All failed, the only webs that survived are those Wordpress websites I built for small business clients. Maybe they survived because those owners know nothing about web technology. But they told me their customers referred to their websites and I get good feedback. I abandoned them anyway because it's not making much money. As for the webs I created from scratch, no framework used, pure PHP, pure javascript and each takes a year to complete, I got only one regular visitor: me. The other one is an experiment which I got some interested from several developers. They were curious and signed up just to see what I'm doing and didn't comeback. The experiment is what become streaming business currently. All are time wasting and take more than six months to do. I know what do you mean walking away. I see many inspired web developers abandoned their pursuits when they got reality checked. Most of webs they created are not maintained and deserted. So the next one I build would be for myself first. I'm not satisfied with other people's service so I would create my own and maybe for some few people who interested. Again, I'll do many thing against deathshadow's suggestions. So @deathshadow, sorry about that. I like your comments and opinions though.
Don't encourage @deathshadow - his behavior may escalate to arson. Then what would we do? I'm also like you. I use Vanilla JavaScript; frameworks are for little girlie-men who burst out crying when they can't understand what an unterminated string literal is, or for those who use a computer mouse as though it were a foot pedal.
+= is to append to end of a string, array.push will push to end of an array you could also do something like myArray[myArray.length + 1] = 'new';
Thanks guys, I found an answer here: https://dev.to/uilicious/javascript...P09sUrLmUED0rw7foLoW7eduEnnaMJhJKNSAyuYJ7jAew