Multiple require () from the same library in the same module

I look at the @ slack / client npm package sources for NodeJs and see that they have this at the top

var forEach = require('lodash').forEach;
var bind = require('lodash').bind;
var has = require('lodash').has;
var isArray = require('lodash').isArray;
var isEmpty = require('lodash').isEmpty;
var isObject = require('lodash').isObject;

What is the point in cherry picking all of this from the lodash module when you can make it more concise by only including the entire library once and then using the methods you need?

// Include the whole lib
var _ = require('lodash');

// And later
if (_.isObject(...)) // etc

It doesn't seem like they use each method many times. In fact, most of them are used once or twice. In addition, I understand that even with a partial requirement of a part of the module, all this is eval()' d, so there is no advantage or performance memory.

I think this package is very well written, so I'm curious to know why they decided to do this.

+4

Source: https://habr.com/ru/post/1671100/


All Articles