Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How js reduces scope lookup

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "js how to reduce scope search", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "js how to reduce scope search" this article.

Reduce scope lookup

(1) do not expose the code to the global scope

For example, the following code runs in the global scope:

Var map = document.querySelector ("# my-map"); map.style.height = "600px"

Sometimes you need to write a script directly on the page, note that in a script tag, the context of the code is global scope, because the global scope is more complex, the search is relatively slow. For example, in the map variable above, when the second line is used, you need to look for this variable in the global scope. Assuming that map is used in a loop, there may be efficiency problems. So it should be made into a local scope:

Function () {var map = document.querySelector ("# my-map"); map.style.height = "600px";} ()

The above uses a function to create a local scope, or you can use the block-level scope of ES6. Because the variable map is directly hit in the current local scope, there is no need to look up the next level of scope (in this case, the global scope), and the local scope search is very fast. At the same time, defining variables directly in the global scope will pollute the window object.

(2) Don't abuse closures.

The purpose of a closure is to allow the child scope to use its parent-scoped variables, and these variables are not visible in different closures. As a result, when looking for a variable, if the current scope is not found, you have to look up to its parent scope, level by level until it is found, or the global scope has not been found yet. So the deeper the closure is nested, the longer it takes to find the variable. As follows:

Function getResult (count) {count++; function process () {var factor = 2; return count * factor-5;} return process ();}

The above code defines a process function in which the search time of count variables is higher than that of local factor variables. In fact, it is not suitable to use closures here. You can pass count directly to process:

Function getResult (count) {count++; function process (count) {var factor = 2; return count * factor-5;} return process (count);}

In this way, the search time of count is the same as that of factor, which is directly hit in the current scope. This tells us that if a global variable needs to be used frequently, it can be cached with a local variable, as follows:

Var url = "; if_ (window.location.protocal = =" https: ") {url =" wss://xxx.com "+ _ window.location.pathname + _ window.location.search;}

The _ window.location object is used frequently, so you can cache it first:

Var url = ""; var location = _ window.location; if (location.protocal = "https:") {url = "wss://xxx.com" + location.pathname + location.search;}

It becomes a local variable so that the lookup is significantly faster than the global lookup, and the code can be written a little less.

The above is all the content of the article "how to reduce the scope of js search". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 276

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report