Install lodash with Angular 4 only to get rid of it

Lately I was still working with ng1.x applications and using Javascript librairies like Lodash and Moment.

Switchin to next versions, it’s very simple for an Angular 4 application to integrate (providing the types exists) and to develop with !

For Lodash, we just need to install both the lib and the types :

npm install --save lodash
npm install --save-dev @types/lodash

Just import in your component the library and use it :

import * as _ from 'lodash';
var tmpHero: Hero = _.find(heroes, (hero) => hero.id == id);
Lodash code suggestion in Visual Studio Code

Now get rid of Lodash

As many of you know, native Javascript is evolving, and just like jQuery slowly became irrelevant, it’s likely that other libraries will. Reasons for that could be the pure performances or the simplicity of usage.

Speaking of performance, you might want to check that kind of test that compares native Date with Moment functions calls :

As of Lodash, you might want to dig into the Array native methods that include powerfull stuff like the now infamous map() function that is used everyday with Observables. (sorry french link to Mozilla Dev Network … )

And you can replace most functions with native methods :

var tmpHero: Hero = _.find(heroes, (hero) => hero.id == id);
// becomes
var tmpHero: Hero = heroes.find((hero) => hero.id == id);

Here is a nice video about replacing Lodash with native :

Not all situations allow to get rid of such libraries, but I think it’s worth digging into native stuff, especially for Angular apps that already come with a nice complexity.