1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

+= vs push | which one is better to add data to an array

Discussion in 'JavaScript' started by ketting00, Jan 20, 2020.

  1. #1
    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,
    SEMrush
     
    ketting00, Jan 20, 2020 IP
    SEMrush
  2. scu8a

    scu8a Greenhorn

    Messages:
    81
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    15
    #2
    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.
     
    scu8a, Jan 22, 2020 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    725
    Likes Received:
    20
    Best Answers:
    3
    Trophy Points:
    118
    #3
    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?
     
    Last edited: Jan 22, 2020
    ketting00, Jan 22, 2020 IP
    scu8a likes this.
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,131
    Likes Received:
    1,696
    Best Answers:
    239
    Trophy Points:
    515
    #4
    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.
     
    deathshadow, Jan 23, 2020 IP
    scu8a likes this.
  5. scu8a

    scu8a Greenhorn

    Messages:
    81
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    15
    #5
    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.
     
    scu8a, Jan 23, 2020 IP
  6. ketting00

    ketting00 Well-Known Member

    Messages:
    725
    Likes Received:
    20
    Best Answers:
    3
    Trophy Points:
    118
    #6
    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.
     
    Last edited: Jan 23, 2020
    ketting00, Jan 23, 2020 IP
  7. scu8a

    scu8a Greenhorn

    Messages:
    81
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    15
    #7
    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.
     
    scu8a, Feb 10, 2020 IP
    ketting00 likes this.