For sure it can be improved rewriting it using new ES6 features like Set and Map. It would be nice to have a benchmark test.