two tricks to improve the javascript performance

104 阅读1分钟

perface 

as many other languages , js  use the Heap and Stack to store the varibale , function and object ,  and we all know that the speed of stack is much  faster  than head. and js

store all the basic data type in the stack and the Reference data type in the heap. there only one Reference data type in js is Object. The Array and the Function(there is the callee property in the function that allows we to call it)   is that  sub type of the Object. See the two examples follow.


Run it , and it spends 100 millisecond. 

Run it, and it spends 30 millsecond(because we visit the c is faster than the a.b) , is third compare to the before. In addition , if you put the a={b:'b'} outside the function b, it only spends 10 millsecond , just tenth .

So , if  we need to use the object property  more than once, let's  assignment it to a varibale. And there is another important rule we must know: always use the minimum.

The a=location.href spends the 500 and the below spends the 800.