So, after about 1 month of #omnixent public beta, here's what I learned about making #Nodejs really, really fast and leaving a low footprint on your server.

A thread 👇
1️⃣ Stop thinking of #JavaScript as an imperative language. Approaching it as a functional language (adopting methods such as map/reduce) makes it really, really fast and safe. Even when working on objects, I do prefer the FP approach (Object.keys(obj).reduce(...)).
2️⃣ Make it lazy. Strict evaluation might be faster, but once your arrays and objects start to get a huge number of entries/properties, iterating over them will slow down the process by a lot. Use Set/Map OR hashmap + lazy properties. You will be computing values when you need
3️⃣ Make it deterministic. Using pure functions will allow you to memoize expensive function calls and computations
4️⃣ Avoid external libraries. #Lodash, #Ramda, etc., are awesome, but with #ESNext, you can probably solve your problems without the need for _.get and other methods.
5️⃣ Use #TypeScript. It's a pain, and we all agree on that, but type safety is something you don't want to negotiate with.
6️⃣ Think parallel. Pure functions allow you to parallelize your code with ease by using worker threads, promises, clustering, etc.
7️⃣ Try to understand how the event loop works. Don't be afraid to use process.nextTick if you really need to, just avoid use timeouts to free the stack before allocating a new call
8️⃣ Let your functions accept one argument only. If you really need to pass multiple values, use currying or pass a statically typed object with TypeScript. It will be easier for you to think about your function.
9️⃣ Shit happens. If you cannot connect to #Redis or any other external data source, make sure that any call to that source results in a no-op.
1️⃣0️⃣ Shit happens pt.2. Wrap your uncertain call into an async function. Always test for failure when spawning your functions. They will fail on a separate process leaving the main thread free from exceptions
You can follow @MicheleRivaCode.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: