accessing the arguments object is expensive.. huh?

后端 未结 2 606
失恋的感觉
失恋的感觉 2021-02-08 13:04

I\'ve heard alot of people saying that accessing the arguments object is expensive. (example: Why was the arguments.callee.caller property deprecated in JavaScript?)

Btw

相关标签:
2条回答
  • 2021-02-08 13:30

    The big deal is at least twofold:

    1) Accessing the arguments object has to create an arguments object. In particular, modern JS engines don't actually create a new object for the arguments every time you call a function. They pass the arguments on the stack, or even in machine registers. As soon as you touch arguments, though, they have to create an actual object. This is not necessarily cheap.

    2) Once you touch the arguments object, various optimizations that JS engines can otherwise perform (e.g. detecting cases in which you never assign to an argument and optimizing that common case) go out the window. Every access to the function arguments, not just ones through arguments becomes much slower because the engine has to deal with the fact that you might have messed with the arguments via arguments.

    0 讨论(0)
  • 2021-02-08 13:35

    I have also never heard a serious explanation for why accessing the arguments object is expensive. However, this site: http://www.playmycode.com/blog/2011/03/simple-yet-effective-javascript-optimisations/ notes that arguments is not really an array and is less efficient than accessing an array. The above linked site even suggests converting arguments to an array as an optimization.

    Going to check with those who know JS interpreters more intimately...

    0 讨论(0)
提交回复
热议问题