javascript - Is there a performance difference between import * as _ from 'lodash' and import { indexOf } from 'lodash' -
i wondering if there difference in memory , performance between 2 import.
if have lodash in node module, compile file anyway no matter import?
in theory, based on specification import
, yes, there supposed difference.
the specification allows compliant optimization use static analysis of named import in order load required provide indexof()
, if lodash module written es2015 module.
it create importentry
record keeps references how resolve import
when running static analysis on es2015 module, relevant export
's evaluated.
in practice, not simple, , since there not native implementation, transpilers babel convert es2015 import
syntax commonjs functional equivalent.
unfortunately, functionally equivalent method must still evaluate entire module, since exports not known until has been evaluated.
this why es2015 specification requires import
, export
in top-level scope, static analysis allow javascript engine optimize determining portions of file can safely omitted when evaluating code export
.
on other hand, there non-native bundlers rollup , webpack perform static analysis in order tree-shaking , remove sections of dead code not referenced import
's module within bundle. optimization independent of use of import
/ export
, using named imports
instead of glob stars allows easier , more optimal static analysis occur within these bundlers, , native implementation released in future.
tl;dr
in theory, yes there difference, in practice, there isn't difference until native implementations available import
/ export
, or unless use bundler performs static analysis , tree-shaking independent of actual syntax , specification.
in case, recommended use named imports, can incur possible optimizations in whatever environment you're using.
Comments
Post a Comment