Nodejs Download Slow

1 view
Skip to first unread message

Janae Chebret

unread,
Jul 23, 2024, 10:21:32 PM7/23/24
to sandplemacmis

Many performance-related issues in Node.js applications have to do with how promises are implemented. Yes, you read that right. How you implemented promise in your Node.js app is most likely the culprit for how slow your app has become ?.

Thank you @vectormike40 for your comment. Yes, while all application that block the event loop will have slow response time the converse is not always true and that is where the confusion is here. It is not sufficiently shown in the article where or how the event loop is blocked and I'm going to update that part of the article. Thanks once again

nodejs download slow


Download File ✔✔✔ https://bltlly.com/2zIxkg



This practical HTTP benchmark sends a significant number of requests to various routes, returning JSON, plain text, anderrors, taking express and fastify as references. The primary objective is to determine if the results obtained fromthe Node.js Internal Benchmark and nodejs-bench-operations areapplicable to common HTTP applications.

require() (or module.require) has long been a culprit of slow Node.js startup times. However, recent performanceimprovements suggest that this function has been optimized as well. Between Node.js versions 18 and 20, we observedimprovements of 4.20% when requiring .js files, 6.58% for .json files, and 9.50% when readingdirectories - all of which contribute to faster startup times.

Those improvements got even better on Node.js 20, with a performance improvement of 25% in comparison to Node.js 18.See the full results in thestate-of-nodejs-performance-2023 repository.

The good news is that the nodejs-bench-operations repository includes a comparison of these methods, which sheds lighton their performance characteristics. In fact, this benchmarking data reveals that the property access in Node.js 20 hasseen significant improvements, particularly when using objects with writable: true andenumerable/configurable: false properties.

The Node.js performance team(nodejs/performance) hasexpanded its scope, leading to greater contributions in optimizing performance with each new version. This trendindicates that Node.js will continue to become faster over time.

I've really been enjoying the new async/await syntax that can be leveraged in recent JavaScript transpires such as Babel and TypeScript but when it happens so fast on a local development environment, some U.I. interactions could get tough to test out. So how can we slow this down or more appropriately add some stall time?

This blog explores common use cases that make expressjs applications and for that matter some nodejs applications, slow. We will take a different approach to only list known facts, and provide quick fixes whenever possible.

Even though this blogpost was designed to offer complementary materials to those who bought my Testing nodejs Applications book, the content can help any software developer to tuneup working environment. You use this link to buy the book.

Finally, we can incredibly slow down promise execution if we use Async Hooks, a deprecated but widely used NodeJS feature. Async hooks help us track asynchronous resources. For example, a tracing library might desire to track a request across callbacks and promises.

To understand the cost of async hooks, I added them to the GraphQL benchmark using Apollo Server. We run the same benchmark as above with AsyncLocalStorage (another slow, but supported feature), and with Async Hooks.

Once you DO know which queries are the important (and slow) ones, you add a very specific resolver for those and developers consuming the api will now be able to use that resolver for improved performance.

In this state SonarScanner is slow (6 files per second). To speed it up: Remove the typings directory either through removing it in the tsconfig, removing the directory itself or copy all files from typings/eit to typings and remove the eit directory.
After that SonarScanner scans 30 files per second which is 5 times faster than before!
With out original project the scanner scans 2-3 files per second.

Performance is one of the most important aspects of web application development.A fast application will make its users, developers, and business stakeholdershappy, while a slow one is sure to frustrate all three parties.

A cache is a high-speed storage layer used as a temporary store for frequentlyaccessed data. You don't have to retrieve data from the (usually much slower)primary source of the data every time it is requested.

When building Node.js applications, timeouts are amongst the easiest things toget wrong. Your server is probably talking to other external services thatthemselves might also be calling other services. Ifone service in the chain is slow or unresponsive, it will result in a slowexperience for your end-users. Even if you don't run into this issue duringdevelopment, you can't guarantee that your dependencies will alwaysrespond as fast as they usually do, which is why the concept of timeouts isimportant.

Before choosing a timeout value, you can monitor the response times for APIs youconnect to using specialized tools or track your API calls by logging them. Thiswill allow you to make an informed decision for all the external services thatyour program interacts with. You should also have a retrystrategy inplace for important services to account for temporary slowdowns. The graph belowshows how average the response times for an endpoint can be monitored inAppSignal.

Clusteringis a technique used to horizontally scale a Node.js server on a single machineby spawning child processes (workers) that run concurrently and share a singleport. It is a common tactic to reduce downtime, slowdowns and outages bydistributing the incoming connections across all the available worker processesso that available CPU cores are utilized to their full potential. Since aNode.js instance runs on a single thread, it cannot take advantage of multi-coresystems properly - hence the need for clustering.

When running MongoDB in production, you may see queries that should be fast, but instead are exceedingly slow. For example, my Node.js apps have seen a findOne() on a collection with only 1 document take over 1 second.

There's a simple explanation for this phenomenon: a MongoDB server can only execute a single operation on a given socket at a time. In other words, the number of concurrent operations your Node.js connection can handle is limited by the poolSize option. For example, the 2nd query below will take approximately 1 second, because poolSize = 1 and it is blocked by a slow query.

You can visualize this by imagining db as a set of poolSize train tracks. If there's only 1 track and a slow, overburdened cargo train takes 1 second to clear the track, the bullet train behind it needs to wait.

Increasing poolSize is a one-liner in both the MongoDB Node.js driver and Mongoose. poolSize = 1 is good for experimenting with the slow train problem, but in production you should use at least the default poolSize = 5. Below is an example with poolSize = 10.

Increasing poolSize won't help if you get a large batch of slow queries all at once. But what can help is separate connection pools for operations that are performance sensitive versus operations that can be slow. For example, in the below example there's two connections db1 and db2. A slow query on db1 doesn't block MongoDB from responding to a fast query on db2.

MongoDB aggregations count as a single operation. Because the aggregation framework is so expressive, many people end up creating exceedingly complex aggregations that end up causing slow trains in production.

In general, if you have an aggregation that's causing slow trains in production, you should consider replacing the aggregation with Node.js logic that relies on find(). For example, you could replace $lookup with Mongoose populate.

MongoDB indexes are a complex subject, but for the purposes of this article the idea is simple: make a slow query fast. For example, you can speed up the $lookup aggregation above using an index on fooId.

The maxTimeMS option tells the MongoDB server to stop running a query after a certain period of time. In the below example, the slow aggregate() call will throw an error after approximately 10ms. Because the MongoDB server stops executing the slow aggregation after 10ms, it unblocks the fast findOne().

One nice detail of maxTimeMS is that it does not count time spent blocked behind a slow train. In other words, if you run a find() with maxTimeMS = 100, and that find() spends 500ms blocked behind a slow query and then executes in 50ms, you won't see an error, even though your Node.js process waited 550ms for the query. This means maxTimeMS helps you find queries that are actually slow, as opposed to queries that appear slow because they're blocked by other slow queries.

A client centric application tends to generate a lot more network traffic than a server centric application that sends just the end result of the processing to the client.
This client centric application will often make more network calls, each involving more data, than the server centric application.
As network latency is by now often the largest performance bottleneck for applications, that often leads to slower client side performance.

Another thing to consider is security. Pretty much every bit of validation you do on the client will need to be repeated on the server. If not you get the infamous situation where a customer fills his shopping cart on the client, edits the amount to be paid to be negative, then submits the whole thing and the server, blissfuly unaware anything is amiss pays the client AND sends him the products.
This may or may not be an issue for your system, depending on what it does and how it is intended to be secured.

Overall then, you're better off limiting the amount of data being transmitted between client and server both in volume and possibly number of requests (a high number of small requests might make for a seemingly faster user interface, think partial page submits, despite being technically slower in total CPU time needed).

760c119bf3
Reply all
Reply to author
Forward
0 new messages