accessing the arguments object is expensive.. huh?

后端 未结 2 610
失恋的感觉
失恋的感觉 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.

提交回复
热议问题