Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Every byte gets processed in JS? #8

Open
fictorial opened this issue Nov 10, 2017 · 2 comments
Open

Every byte gets processed in JS? #8

fictorial opened this issue Nov 10, 2017 · 2 comments

Comments

@fictorial
Copy link

enet-npm/lib/Packet.js

Lines 52 to 54 in cf3ba62

for (; i < end; i++, c++) {
ENETModule["HEAPU8"][i] = buf.readUInt8(c);
}

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...

@ClosetGeek-Git
Copy link

ClosetGeek-Git commented Jun 3, 2019

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants