## 与JavaScriptV 8中的var相比，const或let的性能是否不同？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (187)

``````function timeit(f, N, S) {
var start, timeTaken;
var stats = {min: 1e50, max: 0, N: 0, sum: 0, sqsum: 0};
var i;
for (i = 0; i < S; ++i) {
start = Date.now();
f(N);
timeTaken = Date.now() - start;

stats.min = Math.min(timeTaken, stats.min);
stats.max = Math.max(timeTaken, stats.max);
stats.sum += timeTaken;
stats.sqsum += timeTaken * timeTaken;
stats.N++
}

var mean = stats.sum / stats.N;
var sqmean = stats.sqsum / stats.N;

return {min: stats.min, max: stats.max, mean: mean, spread: Math.sqrt(sqmean - mean * mean)};
}

var variable1 = 10;
var variable2 = 10;
var variable3 = 10;
var variable4 = 10;
var variable5 = 10;
var variable6 = 10;
var variable7 = 10;
var variable8 = 10;
var variable9 = 10;
var variable10 = 10;

function varAccess(N) {
var i, sum;
for (i = 0; i < N; ++i) {
sum += variable1;
sum += variable2;
sum += variable3;
sum += variable4;
sum += variable5;
sum += variable6;
sum += variable7;
sum += variable8;
sum += variable9;
sum += variable10;
}
return sum;
}

const constant1 = 10;
const constant2 = 10;
const constant3 = 10;
const constant4 = 10;
const constant5 = 10;
const constant6 = 10;
const constant7 = 10;
const constant8 = 10;
const constant9 = 10;
const constant10 = 10;

function constAccess(N) {
var i, sum;
for (i = 0; i < N; ++i) {
sum += constant1;
sum += constant2;
sum += constant3;
sum += constant4;
sum += constant5;
sum += constant6;
sum += constant7;
sum += constant8;
sum += constant9;
sum += constant10;
}
return sum;
}

function control(N) {
var i, sum;
for (i = 0; i < N; ++i) {
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
sum += 10;
}
return sum;
}

console.log("ctl = " + JSON.stringify(timeit(control, 10000000, 50)));
console.log("con = " + JSON.stringify(timeit(constAccess, 10000000, 50)));
console.log("var = " + JSON.stringify(timeit(varAccess, 10000000, 50)));
``````

``````ctl = {"min":101,"max":117,"mean":108.34,"spread":4.145407097016924}
``````

``````for (var i = 0; i < 10000; ++i) {
console.log(i);
}
``````

``````for (let i = 0; i < 10000; ++i) {
console.log(i);
}
``````

``````{
let counter = 0;
while (counter < 10000) {
let i = counter;
console.log(i);
counter = i + 1;
}
}
``````

``````function usingVar() {
for (var i = 0; i < 3; ++i) {
setTimeout(function() {
console.log("var's i: " + i);
}, 0);
}
}
function usingLet() {
for (let i = 0; i < 3; ++i) {
setTimeout(function() {
console.log("let's i: " + i);
}, 0);
}
}
usingVar();
setTimeout(usingLet, 20);``````

``````function foo() {
var i = 0;
while (Math.random() < 0.5) {
++i;
}
return i;
}
``````

### 腾讯云 COS 怎么才能外链调用 m3u8 到别的网站播放？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### Ubuntu搭建的WordPress如何修改php.ini？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

php新手很多不知道怎么查配置文件在哪，这里提供一个很简单的方法 使用 php -i 命令可以打印php的详细信息，可以把这堆东西输出一下 php -i > outputphp.txt，结合 grep 查找命令 php -i| grep php.ini 打印结果如下 Config...... 展开详请

### 归档存储采用的存储介质是什么, 安全可靠吗？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### redis数据库应该怎样连接？？？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### 如何使用holer实现从外网访问本地WEB应用？

Dingda

Dingda · 站长 (已认证)