4 ms·
node is meant to be used in scenarios when you give most of the work to libuv. However, if you to CPU intensive work in JavaScript itself, such as: - serializa
by partycoder 10y ago
node is meant to be used in scenarios when you give most of the work to libuv. However, if you to CPU intensive work in JavaScript itself, such as:
- serialization
- encoding
- cryptography
- compression
- etc.
... then, you are better off doing it on a lower level language. express logger counts as a form of serialization, and therefore it is slow.
If you absolutely have to do CPU intensive work... then: limit per-tick execution time and break it down into multiple ticks, otherwise you will block the node event loop and your system will degrade until it stops processing requests.
Now, you can find this faster by profiling. Just use a profiler, like nodegrind or flame graphs or whatever, you will find those bottlenecks very quickly.
- sjnair96 10y agoIf you don't mind, could you explain in detail (or link to resources) the points you bring up? It seems extremely interesting.
- amelius 10y agoWell, it boils down to the fact that libuv (part of Nodejs) helps in fair scheduling for I/O resources; but the CPU is a resource too, and there is no scheduling taking place for that resource. Therefore, heavy CPU use without proper yielding can block other activities. Since the CPU is involved even in starting I/O operations, heavy CPU use can also block I/O, and in fact it can block the whole process.
- aikah 10y agoOr just ditch node and write your server in Go. There is no reason to use node server side today, unless that task is specifically javascript related. There is 0 advantage in using nodejs.
- Veen 10y agoOr Elixir.
- roelvanhintum 10y agoA universal React application would be one...
- tboyd47 10y agoInteresting - does template rendering also count as serialization?
- tedd4u 10y agoYep. You can benchmark your server with template rendering and then benchmark again just outputting the values you plug into the templates without rendering them to get a sense of what it's costing you in terms of throughput, latency, etc. The advantage of node.js for serving websites I believe lies in the reusability of JS code on server and web browser, and that outweighs this issue. But you have to make sure your template rendering is efficient enough or be willing to spread the load across enough cores.