Using _.uniqueId in lodash, and alternatives

Writing more content on lodash this month for now, and have noticed that I did not get around to _.uniqueId yet. The method addresses something that comes up a lot now and then when developing projects, so it deserves a post. Also in these lodash posts I often take a moment to brush up on how hard it is to go about making a vanilla js solution, or if there are native methods that can be used, and as such this post will be no exception. So lets take a look at _.uniqueId, and some other solutions for generating unique ids.

1 - What to know

This is a post on the _.uniqueId method in lodash. This method can be used to create a unique id for something each time it is called. This method will work for the purpose of assigning a unique id for most use cases, but not with complex projects that require a unique id always even if the project restarts. In any case this will be a brief post on this topic.

2 - An _.uniqueId example

Using the _.uniqueId method is fairly straight forward to use, I just need to call it, passing an optional prefix as the first argument, and a unique id will be returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
let _ = require('lodash');
let id = _.uniqueId('id_');
console.log(id); // 'id_1'
let i = 10, ids = [];
while (i--) {
ids.push(_.uniqueId('id_'));
}
console.log(ids[0]); // id_2
console.log(ids[9]); // id_11

3 Vanilla js alternative to _.uniqueId

Making my own solution for this is not to hard, all is needed is the power of closure.

1
2
3
4
5
6
7
8
9
10
11
12
let uniqueId = (function () {
let num = 0;
return function (prefix) {
prefix = String(prefix) || '';
num += 1;
return prefix + num;
}
}
());
let id = uniqueId('id_');
console.log(id); // 'id_1'