JavaScript inner-functions and Performance

后端 未结 2 1314
轻奢々
轻奢々 2020-12-19 11:32

What is the impact on running-time and memory defining a clousre vs. global-scope function?

function a(){
      //functions (option A)
}
//functions(option B         


        
2条回答
  •  既然无缘
    2020-12-19 12:11

    Performance

    A very tiny benchmark case:

    #1 inner function: http://jsfiddle.net/bMHgc/

    #2 function outside: http://jsfiddle.net/sAVZn/

    At my machine: (5000 * 1000 times)

    #1 - 700ms

    #2 - 80ms

    Memory

    They are almost the same ...

    I would recommend option A, if possible, since it can make your code cleaner.

提交回复
热议问题