Skip to content

Commit

Permalink
Added 'Native methods Over Util' article
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkmann18 committed Dec 25, 2018
1 parent 7846d3e commit ea7617c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,14 @@ All statements above will return false if used with `===`

## Our contributors are working on this section. [Would you like to join?](https://github.com/i0natan/nodebestpractices/issues/256)

## ![] 7.1. Prefer native JS methods over user-land utils like Lodash

**TL;DR:** It's often more penalising to use utility libraries like `lodash` and `underscore` over using native methods as it leads to uneeded dependencies with less performance boost.

**Otherwise:** You'll have to maintain (slightly) bigger projects where you could have simply used what was **already** available or dealt with a few more lines in exchange of a few more files.

🔗 [**Read More: Native over user land utils**](/sections/performance/nativeoverutil.md)

<br/><br/><br/>

# Milestones
Expand Down
90 changes: 90 additions & 0 deletions sections/performance/nativeoverutil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Prefer native JS methods over user-land utils like Lodash

<br/><br/>

### One Paragraph Explainer

Sometimes, using native methods is better than requiring `lodash` or `underscore` because the resulting program will have more dependencies and thus use more space and also not guarantee the best performance all-round.

<!-- comp here: https://gist.github.com/Berkmann18/3a99f308d58535ab0719ac8fc3c3b8bb-->

<br/><br/>

### Code Example – Benchmark test on `_.concat`/`Array.concat`

```javascript
const _ = require('lodash'),
__ = require('underscore'),
Suite = require('benchmark').Suite,
chalk = require('chalk');

function onComplete() {
let fastest = String(this.filter('fastest').map('name')),
slowest = String(this.filter('slowest').map('name'));
console.log(`\tBenchmark: ${chalk.cyan(this.name)}\nThe fastest is ${chalk.black.bgGreen(fastest)}\nThe slowest is ${chalk.black.bgRed(slowest)}\n`)
}
const onCycle = event => console.log(`${event.target}`);
const opts = {
onComplete,
onCycle
};

const concatSuite = new Suite('concat', opts);
const a0 = [1];

concatSuite
.add('lodash', () => _.concat(a0, 2, [3], [
[4]
]))
.add('underscore', () => __.concat(a0, 2, [3], [
[4]
]))
.add('native', () => a0.concat(2, [3], [
[4]
]))
.run({ 'async': true });
```

Which returns this:
```bash
lodash x 1,896,368 ops/sec ±5.64% (89 runs sampled)
underscore:
native x 2,488,685 ops/sec ±6.46% (86 runs sampled)
Benchmark: concat
The fastest is native
The slowest is lodash
```
You can find a bigger list of benchmarks [here](https://github.com/Berkmann18/NativeVsUtils/blob/master/index.txt) or alternatively [run this](https://github.com/Berkmann18/NativeVsUtils/blob/master/index.js) which would show the same but with colours.

### "You don't (may not) need Lodash/Underscore"

From the [repo on this matter which focuses on Lodash and Underscore](https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore).

> Lodash and Underscore are great modern JavaScript utility libraries, and they are widely used by Front-end developers. However, when you are targeting modern browsers, you may find out that there are many methods which are already supported natively thanks to ECMAScript5 [ES5] and ECMAScript2015 [ES6]. If you want your project to require fewer dependencies, and you know your target browser clearly, then you may not need Lodash/Underscore.
There's also an [ESLint plugin](https://www.npmjs.com/package/eslint-plugin-you-dont-need-lodash-underscore) which detects where you're using either libraries but don't need to.

Here's an example of that plugin in use:
Consider a file called _lodashLove.js_ shown below
```js
const _ = require('lodash');

let arr = [0, 1, 2, 4, 8, 16];

console.log(_.map(arr, x => `d${x}`));

if (_.includes(arr, 0)) console.log('0 found');

console.log('compacted:', _.compact(arr));
```

Here's what ESLint would output when using the YDNLU plugin.
```bash
/home/maxie/GDrive/Other/Sandbox/YDNLU/lodashLove.js
5:13 warning Consider using the native Array.prototype.map() you-dont-need-lodash-underscore/map
7:5 warning Consider using the native Array.prototype.includes() you-dont-need-lodash-underscore/includes

✖ 2 problems (0 errors, 2 warnings)
```

Of course, the example above doesn't seem realistic considering to what actual codebases would have but you get the idea.

0 comments on commit ea7617c

Please sign in to comment.