在AS3中,我认为你应该在循环之外初始化所有变量以提高性能。JavaScript也是这样吗?哪个更好/更快/最佳实践?
var value = 0;
for (var i = 0; i < 100; i++)
{
value = somearray[i];
}
or
for (var i = 0 ; i < 100; i++)
{
var value = somearray[i];
}
在AS3中,我认为你应该在循环之外初始化所有变量以提高性能。JavaScript也是这样吗?哪个更好/更快/最佳实践?
var value = 0;
for (var i = 0; i < 100; i++)
{
value = somearray[i];
}
or
for (var i = 0 ; i < 100; i++)
{
var value = somearray[i];
}
当前回答
在JavaScript和ActionScript中,在意义和性能上没有任何区别。
Var是解析器的指令,而不是在运行时执行的命令。如果一个特定的标识符在函数体(*)中被声明为var一次或多次,那么该块中所有该标识符的使用都将引用该局部变量。value在循环内部、循环外部或两者都声明为var没有区别。
Consequently you should write whichever you find most readable. I disagree with Crockford that putting all the vars at the top of a function is always the best thing. For the case where a variable is used temporarily in a section of code, it's better to declare var in that section, so the section stands alone and can be copy-pasted. Otherwise, copy-paste a few lines of code to a new function during refactoring, without separately picking out and moving the associated var, and you've got yourself an accidental global.
特别是:
for (var i; i<100; i++)
do something;
for (var i; i<100; i++)
do something else;
Crockford会建议你删除第二个变量(或者删除两个变量并执行var i;), jslint会因此抱怨你。但在我看来,保留两个变量更容易维护,将所有相关代码放在一起,而不是在函数顶部添加额外的、容易忘记的代码。
就我个人而言,我倾向于将变量的第一次赋值声明为var,无论在同一函数的其他部分是否有相同变量名的另一种单独用法。对我来说,必须声明var在所有是一个不受欢迎的JS疣(它会更好的变量默认为本地);我不认为在JavaScript中复制ANSI C(旧版本)的局限性是我的责任。
(*:除了嵌套函数体)
其他回答
在for循环内部或外部声明变量没有区别。 下面是要测试的示例代码。
function a() {
console.log('Function a() starts');
console.log(new Date());
var j;
for (j=0; j<100000000; j++) {
var x;
x = x + 1;
}
console.log(new Date());
console.log('Function a() Ends');
}
a()
function b() {
console.log('Function B() starts');
console.log(new Date());
var a;
var j;
for (j=0; j<100000000; j++) {
a = a + 1;
}
console.log(new Date());
console.log('Function B() Ends');
}
b()
结果显示在我的情况下
Function a() starts
VM121:3 Thu Apr 12 2018 15:20:26 GMT+0530 (India Standard Time)
VM121:9 Thu Apr 12 2018 15:20:26 GMT+0530 (India Standard Time)
VM121:10 Function a() Ends
VM121:14 Function B() starts
VM121:15 Thu Apr 12 2018 15:20:26 GMT+0530 (India Standard Time)
VM121:21 Thu Apr 12 2018 15:20:26 GMT+0530 (India Standard Time)
VM121:22 Function B() Ends
谢谢—— MyFavs.in
理论上讲,这对JavaScript没有任何影响,因为该语言没有块作用域,只有函数作用域。
我不确定关于性能的争论,但是Douglas Crockford仍然建议var语句应该是函数体中的第一个语句。引用JavaScript编程语言的代码约定:
JavaScript没有块作用域,所以在块中定义变量会让熟悉其他C语言的程序员感到困惑。定义函数顶部的所有变量。
我认为他是有道理的,你可以从下面的例子中看到。在函数顶部声明变量不应该让读者误以为变量i被保存在for循环块的作用域中:
function myFunction() {
var i; // the scope of the variables is very clear
for (i = 0; i < 10; i++) {
// ...
}
}
我更喜欢将可读性和性能结合起来。 所以我最喜欢在循环中声明变量,这意味着我将有块作用域封装。
for (let i = 0, sum = 0; i < count; i++) { // count also be declared here like count = array.length;
sum = sum + 1;
}
根据之前提供的小提琴性能测试,获胜者是4号
嗯,这取决于你想要达到什么目标……如果value只是循环块中的一个临时变量,那么使用第二种形式会更清楚。它也更有逻辑性和啰嗦。
另一个需要考虑的问题是,既然我们在ES2015中有了let和const,现在你可以将变量的作用域专门用于循环块。因此,除非你在循环之外需要相同的变量(或者如果每次迭代都依赖于在前一次迭代中对该变量所做的操作),否则最好这样做:
for (let i = 0; i < 100; i++) {
let value = somearray[i];
//do something with `value`
}