mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-29 16:46:01 +08:00
Updated README and a section
This commit is contained in:
@ -958,8 +958,9 @@ All statements above will return false if used with `===`
|
|||||||
## ![✔] 7.1. Prefer native JS methods over user-land utils like Lodash
|
## ![✔] 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 native methods as it leads to unneeded dependencies and slower performance.
|
**TL;DR:** It's often more penalising to use utility libraries like `lodash` and `underscore` over native methods as it leads to unneeded dependencies and slower performance.
|
||||||
|
Bar in mind that with the introduction of the new V8 engine alongside the new ES standards, native methods were improved in such a way that it's now about 50% more performant than utility libraries.
|
||||||
|
|
||||||
**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.
|
**Otherwise:** You'll have to maintain less performant 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)
|
🔗 [**Read More: Native over user land utils**](/sections/performance/nativeoverutil.md)
|
||||||
|
|
||||||
|
|||||||
BIN
assets/images/ydnlu.png
Normal file
BIN
assets/images/ydnlu.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
@ -1,10 +1,20 @@
|
|||||||
# Prefer native JS methods over user-land utils like Lodash
|
# Prefer native JS methods over user-land utils like Lodash
|
||||||
|
|
||||||
|
|
||||||
<br/><br/>
|
<br/><br/>
|
||||||
|
|
||||||
### One Paragraph Explainer
|
### 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.
|
Sometimes, using native methods is better than requiring `lodash` or `underscore` because it **will not lead in a performance boost** and use more space than necessary.
|
||||||
|
The performance using native methods result in an overall ~50% gain which includes the following methods:
|
||||||
|
- `Array.concat`
|
||||||
|
- `Array.fill`
|
||||||
|
- `Array.filter`
|
||||||
|
- `Array.map`
|
||||||
|
- `(Array|String).indexOf`
|
||||||
|
- `Object.find`
|
||||||
|
- ...
|
||||||
|
|
||||||
|
|
||||||
<!-- comp here: https://gist.github.com/Berkmann18/3a99f308d58535ab0719ac8fc3c3b8bb-->
|
<!-- comp here: https://gist.github.com/Berkmann18/3a99f308d58535ab0719ac8fc3c3b8bb-->
|
||||||
|
|
||||||
@ -78,13 +88,8 @@ if (_.includes(arr, 0)) console.log('0 found');
|
|||||||
console.log('compacted:', _.compact(arr));
|
console.log('compacted:', _.compact(arr));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Example: detecting non-v8 util usage using a linter
|
||||||
Here's what ESLint would output when using the YDNLU plugin.
|
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 what actual codebases would have but you get the idea.
|
Of course, the example above doesn't seem realistic considering what actual codebases would have but you get the idea.
|
||||||
|
|||||||
Reference in New Issue
Block a user