Array Unique Part II - I use a lib ...

Whenever I think I invent something - it means I haven’t google enough.

me

Let continue digging to a trivial whiteboard task.

So top 1 quote on the interview about the trivial task.

I use …. library for it.

candidate

So let’s take a look to libs.


(c)

Reminder

Array unique

From last time our winner is a method that uses maps for search. We get O(N) complexity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function uExtraMap (arr) {
var r = [];
var m = {}

var l = arr.length ;
for(var j = 0 ; j < l ; j++) {

if(!m[arr[j]]) {
m[arr[j]] = true;
r.push(arr[j]);
}
}
return r;
}

Why Reinvent a wheel?

Libraries are written and maintained by the big community. It is written by mega smart folks much smarter than you.

Just use XXXX lib

In general, it is true. It is faster to use well tested and checked the library.

The dark side of the lib

  • It is designed to be a lib. Wide generic cases covered
  • It is intended to serve library cases and compositions. It follows patterns of lib.
  • The size.
  • hidden complexity

Lets take 2 top Folks

It is merely one-liners in every library. Simple and elegant.

Lodash

https://lodash.com/ is a seasoned and old continuation of underscore.

1
_.uniq(a10000)

What inside ?!

source code
So as we see 80 lines with banch of imports.

As we could guess from names - sophisticated set implementation is used.

Ramda

Ramda more functional pure and super popular

More fun why

1
R.uniq(a10000)

What inside ?!

As we could see the code is even quite close to what we have))
uniqBy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var set = new _Set();
var result = [];
var idx = 0;
var appliedItem, item;

while (idx < list.length) {
item = list[idx];
appliedItem = fn(item);
if (set.add(appliedItem)) {
result.push(item);
}
idx += 1;
}
return result;
}

Let’s take a look to graph.

Ups ((

The Price

So let’s remove all IE7 unfriendly cases from our test and all deviations of map implementation.
We want to compare or winner with a lodash and ramda.

jsperf test give all code and allow to test on your machine.

As we can see Ramda do much better but still far away.

It is dost mean that our solution is super smart and better, rather opposite. We can see that for concrete cases less mean more. I believe that lodash or ramda code is quite a cold evening reading with a lot of closed cases and sophisticated cool logic and corner cases.

The key question here - is it part of our problem?

Libraries are a focus on general cases.

The lesson learn

  • Read the code of your libraries
  • Understand your code
  • Understand key KPI and requirement of your system or even “Trivial task.”
  • Do not trust the library gods
  • The dark side of dark side - if you spend 80% time to tune your little helper … maybe you are writing yet another monster.

PS

One more cool reading about a topic.
You-Dont-Need-Lodash
As I said, It is not black and white. I showed why ES6 Implementation is not always cool.

Wait wait I use babel

You don’t need to struggle that Old browsers do not support cool stuff

1
2
3
function es6Unique (arr) {
return [...new Set(arr)];
}

Lets use babel

but let’s have a chat next time about it…