A common practice at this time is to connect a minified JS file to the end of the document before the closing body . They say that this is good for performance, but the question arises - how to initialize only those functions and variables that we need for the current web page?

With variables, in principle, I know several solutions, but I cannot say how best to initialize functions. Suppose the minified file includes the n-th number of functions, and when the minified file is loaded, all functions will be initialized, including those that are not needed on this particular page. And maybe a situation may arise when there are about 100 functions in a minified file, and only 2-3 are needed on a page.

Judging by the fact that this is not really trying to avoid, we can make the assumption that loading functions without calling them is not a very heavy operation in terms of performance, even if these functions are many. Is this a true assumption?

  • This is all the harmful influence of hipsters, just scatter functions across different js-files and connect only the necessary ones, and let gzip embedded in nginx minify ;) andreymal
  • JS-files are connected at the end of the page not in terms of processor performance, but in terms of page loading speed. So that the page is already shown, and the scripts are loaded later, after the content is already loaded - Crantisz

1 answer 1

  1. The initialization of the function costs a bit (although one uncle with a mustache indicates some relativity).
  2. Do not ship what is not needed. Or ship via async and defer .
  3. Connecting scripts in the end is not done for this. Scripts stop parsing, read on the topic .