Summary of JavaScript implementation of high-performance data storage

怪我咯
Release: 2017-03-29 14:41:30
Original
1174 people have browsed it

1.JavaScriptFour basic data access locations: literal, localvariable,arrayelement,objectmember.

Generally speaking: [literal, local variable] running speed > [array, object member]

2. Internalattributescontain A collection of objects in the scope in which afunctionis created. This set is called a scope chain.

3. Execute the function->Create execution environment->Create active object (i.e. function runtime variable object).

So calling the same function multiple times will result in the creation of multiple execution environments.

4. Function execution process

Every time a variable is encountered, it will go through an identifier resolution process, where to obtain or store data. This process searches the scope chain of the execution environment. It is this search process that affects performance.

5. Performance of identifier parsing

Global variables always exist at theend of the execution environment scope. Local variables are resolved first.

Rule of thumb: If a cross-scope value is referenced more than once in a function, store it in a local variable.

For example:

function initUI(){ var bd=document.body; //后面有多次doucument这个全局对象的调用 } //->优化后 function initUI(){ var doc=document; bd=doc.body; //把doucument这个全局对象的引用存储到局部变量doc中 }
Copy after login



6. Change the scope chain

Generally speaking, a The scope chain of the execution environment will not change.

<1>with can temporarily change the scope chain

widthUsed to create a variable for all properties of the object

function initUI(){ with(document){ var bd=body; } }
Copy after login



当代码执行到with时,执行环境的作用域链被临时改变了。一个新的变量对象呗创建,它包含了参数指定对象的所有属性。这个对象呗推入作用域链的首位,所以这时候所有的局部变量处于的哥第二个作用域链对象中,因此访问代价更高了。

<2>try-catch

try语句发生错误的时候,执行过程会自动跳转到catch中。然后把异常对象推入一个变量对象并置于作用域的首位。

注意:一旦catch子语句执行完毕,作用域链就会返回到之前的状态。

7.闭包引发的性能问题

闭包是JavaScript最强大的特性之一。

由于闭包包含了执行了与环境作用域链相同对象的引用,函数的活动对象不会被销毁,造成更多的内存开销。

关注的性能点:频繁访问跨作用域的标识符时,每次访问都会带来性能损失。

Start:19:41:45 2015-11-21 引用自by Aaron:/content/3493261.html

8.内存泄露

内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束。在C++中,因为是手动管理内存,内存泄露是经常出现的事情。而现在流行的C#和Java等语言采用了自动垃圾回收方法管理内存,正常使用的情况下几乎不会发生内存泄露。浏览器中也是采用自动垃圾回收方法管理内存,但由于浏览器垃圾回收方法有bug,会产生内存泄露。

内存泄露的几种情况

循环引用

Javascript闭包

DOM插入顺序

一个DOM对象被一个Javascript对象引用,与此同时又引用同一个或其它的Javascript对象,这个DOM对象可能会引发内存泄漏。这个DOM对象的引用将不会在脚本停止的时候被垃圾回收器回收。要想破坏循环引用,引用DOM元素的对象或DOM对象的引用需要被赋值为null

具体的就深入讨论了,这里的总结

JS的内存泄露,无怪乎就是从DOM中remove了元素,但是依然有变量或者对象引用了该DOM对象。然后内存中无法删除。使得浏览器的内存占用居高不下。这种内存占用,随着浏览器的刷新,会自动释放。

而另外一种情况,就是循环引用,一个DOM对象和JS对象之间互相引用,这样造成的情况更严重一些,即使刷新,内存也不会减少。这就是严格意义上说的内存泄露了。



The above is the detailed content of Summary of JavaScript implementation of high-performance data storage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!