enet-npm icon indicating copy to clipboard operation
enet-npm copied to clipboard

Every byte gets processed in JS?

Open fictorial opened this issue 6 years ago • 2 comments

https://github.com/mnaamani/enet-npm/blob/cf3ba625f1ba1c83a4aff6d322d91d83427d5585/lib/Packet.js#L52-L54

I am not familiar with Emscripten enough to know if there's a way to avoid this loop over every byte received. At this point, you're in JS land and the Buffer API deals with well, JS data so I get it. But I wonder...

fictorial avatar Nov 10 '17 15:11 fictorial

I don't know this as a fact, but most modern JS engines compile JS directly into machine instructions where possible. This doesn't matter much with many types of JS objects because they don't have true native equivalents (no processor has a native Date data type for example) but chars are basic int types that are usually processed using basic arithmetic operators that have native equivalents. So a modern JS compiler can optimise this loop and than convert it to true machine code because of how basic it is, so no not every byte is processed in JS space.

It would be a different situation however if this loop was trying to send one byte at a time. Even though the lower IP layer will buffer it and not truly send it until flushed, the network object (websocket for example) isn't converted to native instructions so you still have all the glue that it takes to communicate between JS space and the underlying library and tying to do so one byte at a time would be a waste.

This is off the top of my head but I think it's accurate

ClosetGeek-Git avatar Jun 03 '19 07:06 ClosetGeek-Git

This is how modern JS is. Some code can be broken down to the point that it would take anal assembly developer to write the equivalent, yet other parts of a program might just barely out perform PHP. Your usually pretty safe however with the way JS breaks down and optimises basic string operations like this.

ClosetGeek-Git avatar Jun 03 '19 07:06 ClosetGeek-Git