简体   繁体   English

如果未使用javascript函数中的参数,会降低性能吗?

[英]does it slow performance to declare parameters in a javascript function if they are not being used

I am using a plugin which monitors the dragging of an element. 我正在使用一个监视元素拖动的插件 As the coordinates of the element change when it is dragged, several parameters are made available to an event listener function: 当元素的坐标在拖动时发生变化时,事件侦听器函数可以使用几个参数:

$el.on('dragMove',onDragMove);

onDragMove = function(e,pointer,vector){
  //
}

Is there any performance benefit to removing those parameters if I am not using them within the function: 如果我不在函数中使用这些参数,删除这些参数是否会对性能产生任何好处:

onDragMove = function(){
  //
}

?

IMHO If there is any performance impact, you can ignore it. 恕我直言,如果有任何性能影响,您可以忽略它。 But for the syntax-sugar and code quality it is always better to define only what you need. 但是对于语法糖和代码质量,最好只定义所需的内容。 Some code quality tools (JSLint for example) and editors will highlight it for you when you have parameters defined that you don't use inside the function. 当您定义了不在函数中使用的参数时,某些代码质量工具(例如JSLint)和编辑器会为您突出显示它。

There is minimal impact on performance due the low memory consumption of declaring the arguments as variables, since the Javascript plugin you are using is still sending to the arguments at those positions. 由于将参数声明为变量的内存消耗很低,因此对性能的影响最小,因为您使用的Javascript插件仍会发送到这些位置的参数。 Ultimately it would depend on the received data, as the function arguments, other than objects , are passed by value and not by reference. 最终,它将取决于接收到的数据,因为除对象之外的函数参数是通过值而不是通过引用传递的。

The biggest benefit to removing the declarations, would be more so for readability of your code. 删除声明的最大好处是代码可读性更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM