-
I'm using uWebSockets server on node.js |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 2 replies
-
You can do it this way.
|
Beta Was this translation helpful? Give feedback.
-
or |
Beta Was this translation helpful? Give feedback.
-
I tested both methods as performance is an issue for me. I wanted to avoid degrading the performance of uWS with inefficient built in methods. Using Bench.js to compare the two it seems TextDecoder loses out. I'd be glad for any input on the quality of the test, the results surprised me. The results are
The code I used is
|
Beta Was this translation helpful? Give feedback.
-
Yes you can do Buffer.from(message).toString() or any such alternative. uWS only deals with ArrayBuffer to/from JS since that representation maps 1:1 with the internals (there is no distinction between "string" and "binary" internally). |
Beta Was this translation helpful? Give feedback.
-
@alexhultman Could you put this in the documentation and samples ? |
Beta Was this translation helpful? Give feedback.
-
Yes it could make sense to update the texts on the docs |
Beta Was this translation helpful? Give feedback.
-
since this thread has been revived, I will point out that:
is about 10-15% faster that And that the slow dog TextDecoder, whilst still a slow dog would be 4 times faster than listed in the bench above if the class wasn't instantiated with every iteration ( and DataView can be dropped ) |
Beta Was this translation helpful? Give feedback.
-
The fastest is of course to never make it a String, only dealing with binary protocols, but I understand JSON is everywhere so sadly this has to happen in most cases. We can add more info in the docs. |
Beta Was this translation helpful? Give feedback.
-
Indeed. String.fromCharCode is a little restrictive with character sets, but can be used to decode arraybuffer on the browser, which might allow the server to not have to deal with strings quite as much in that instance. |
Beta Was this translation helpful? Give feedback.
-
Buffer.from(arrayBuffer).toJSON() should potentially be a fast path for ArrayBuffer -> JSON? |
Beta Was this translation helpful? Give feedback.
-
Yes, it’s default use. JSON.parse(Buffer.from(arrayBuffer)) |
Beta Was this translation helpful? Give feedback.
You can do it this way.