There is a JS file that contains scripts from all pages of the site (there are quite a lot of pages).
Does it make sense to prescribe inside a structure like this:

if (мы на странице N) {то выполнить этот код} else if (мы на странице M) {то выполнить этот код} 

Does this affect the speed of loading / processing by JS browser: will it be faster or not?

    1 answer 1

    Does this affect the speed of loading / processing by JS browser: will it be faster or not?

    Faster than what? What to compare?
    The download speed is affected by the size of the js script. If it is not gigantic and it is convenient for you to have one script, then I would advise not to break it into separate scripts. It's just more convenient for the browser to load one script than several.

    This check will not affect the processing speed. The browser executes the script only when it is requested and only the part that is requested.
    For example, if 'if (5> 3 && 4 <3 && ...) ...' the browser will not check for the rest, the more it will process the action in case of truth.

    So you can safely use your design. But it may be more convenient to use the switch statement.

    • JS files are combined into one when building. Accordingly, the file contains scripts from the entire site. On page A, the function N can be called, and on page B, the function M can be called. So I wanted to know if there would be a difference in speed if I stuff the code of functions N and M (we are on the right page) into the condition. Accordingly, with this and compare. - Floyat