Posts

« All Blogs

Illustration of African American women

Let’s Talk About Sets

https://eng.lifion.com/lets-talk-about-sets-813dfeb2185

 

Let’s Talk About Sets

A re-introduction to JavaScript Sets and the new Set methods

Edgardo Avilés

Mar 1, 2019 · 5 min read

Let’s talk about you and me and how we used to find unique items before ES6. We really only had two ways to do it (if you had another one let me know). On the first one, we would create a new emtpy object, iterate through the items we wanted to deduplicate, we would create a new property using the item as the key and something like “true” as the value, then we would get the list of keys of that new object and we were done. In the second way, we would create a new empty array, iterate through the items, and for each item, check if the item existed in the array, if it was already there, continue, if not, add it. By the end the array would contain all the unique items.

ES6 introduced Sets, a new data structure with a very simple API to handle unique items that is not just convenient but also very fast. The intention of this article is to introduce you to some new methods coming to Sets soon that will make them even more useful, but before, let’s remember the basics.

Here in Lifion we are big users of JavaScript, about 90% of our platform services are Node.js-based. If you are interested to see some examples of how Sets are used in our codebase, check our open source projects in Lifion’s GitHub profile.

The basics of Sets

To create a new set we only need to use the constructor. We can optionally pass any iterator, such as an array or a string, and the iterated items will become elements of the new set (repeated items will be ignored).

const emptySet = new Set();
const prefilledSet = new Set(['

« All Blogs

Person gesturing toward large computer screen

Performance implications of misunderstanding Node.js promises

https://eng.lifion.com/promise-allpocalypse-cfb6741298a7

Promise.allpocalypse

The performance implications of misunderstanding Node.js promises

Ali Yousuf

Ali Yousuf

Jan 22 · 8 min read

for…of over unknown collection with await in loop
Promise.all() on an entire unknown collection

Benchmarking unbounded promise scenarios

╔═══════════════╦══════════════════════════════════╗
║     Test      ║ Average Execution Time (Seconds) ║
╠═══════════════╬══════════════════════════════════╣
║ await-for-of  ║                            6.943 ║
║ bluebird-map  ║                            4.550 ║
║ for-of        ║                            6.745 ║
║ p-limit       ║                            4.523 ║
║ promise-all   ║                            4.524 ║
║ promise-limit ║                            4.457 ║
╚═══════════════╩══════════════════════════════════╝
for…of test code
for await…of test code
Image for post

Image for post

Image for post

Image for post

Clinic.js doctor output for for await…of and for…of, respectively
Image for post

Image for post

Image for post

Image for post

Clinic.js bubbleprof output for for await…of and for…of, respectively
Promise.all() test code
Image for post

Image for post

Clinic.js doctor output for Promise.all()

Promise chain execution order example
Async chain execution order example
Bluebird.map() with concurrency limit test code
Image for post

Image for post

Clinic.js doctor output for Bluebird.map() with concurrency limit
promise-limit module test code
Image for post

Image for post

Clinic.js doctor output for the promise-limit module
p-limit module test code
Image for post

Image for post

Clinic.js Doctor output for the p-limit module

Conclusion