mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-27 10:55:55 +08:00
Updated README and nativeoverutil
Improved the examples and swapped some text with images.
This commit is contained in:
@ -958,7 +958,7 @@ All statements above will return false if used with `===`
|
||||
## ![✔] 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.
|
||||
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.
|
||||
Bear 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 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.
|
||||
|
||||
|
||||
BIN
assets/images/concat-benchmark.png
Normal file
BIN
assets/images/concat-benchmark.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
BIN
assets/images/sampleMeanDiag.png
Normal file
BIN
assets/images/sampleMeanDiag.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 46 KiB |
@ -20,50 +20,31 @@ The performance using native methods result in an overall ~50% gain which includ
|
||||
|
||||
<br/><br/>
|
||||
|
||||
### Code Example – Benchmark test on `_.concat`/`Array.concat`
|
||||
### Example: benchmark comparison - Lodash vs V8 (Native)
|
||||
The graph below shows the mean of the benchmarks for a variety of Lodash methods, this shows that Lodash methods take on average 146.23% more time to complete the same tasks as V8 methods.
|
||||
|
||||

|
||||
|
||||
### 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
|
||||
};
|
||||
opts = require('./utils'); //cf. https://github.com/Berkmann18/NativeVsUtils/blob/master/utils.js
|
||||
|
||||
const concatSuite = new Suite('concat', opts);
|
||||
const a0 = [1];
|
||||
const array = [0, 1, 2];
|
||||
|
||||
concatSuite
|
||||
.add('lodash', () => _.concat(a0, 2, [3], [
|
||||
[4]
|
||||
]))
|
||||
.add('underscore', () => __.concat(a0, 2, [3], [
|
||||
[4]
|
||||
]))
|
||||
.add('native', () => a0.concat(2, [3], [
|
||||
[4]
|
||||
]))
|
||||
concatSuite.add('lodash', () => _.concat(array, 3, 4, 5))
|
||||
.add('underscore', () => __.concat(array, 3, 4, 5))
|
||||
.add('native', () => array.concat(3, 4, 5))
|
||||
.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"
|
||||
|
||||
Reference in New Issue
Block a user