针对循环体变量声明与初始化的效率问题,将执行的简单测试代码如下:

function test(n) {
    console.time('Internally initialized');
    for (var i = 0; i < n; i++) {
        let a = i;
    }
    console.timeEnd('Internally initialized');

    console.time('Externally initialized');
    let a = 0;
    for (var i = 0; i < n; i++) {
        a = i;
    }
    console.timeEnd('Externally initialized');

    console.time('Externally declared');
    let b;
    for (var i = 0; i < n; i++) {
        b = i;
    }
    console.timeEnd('Externally declared');
}

Chromium 58.0.3029.110下的测试结果,没有考虑更多的测试条件、环境因素影响等等:

test(1000000000)
Script snippet #2:6 Internally initialized: 2894.2080078125ms
Script snippet #2:13 Externally initialized: 2867.099853515625ms
Script snippet #2:20 Externally declared: 2895.260986328125ms
undefined
test(100000000)
Script snippet #2:6 Internally initialized: 355.6640625ms
Script snippet #2:13 Externally initialized: 296.761962890625ms
Script snippet #2:20 Externally declared: 296.949951171875ms
undefined
test(10000000)
Script snippet #2:6 Internally initialized: 68.672119140625ms
Script snippet #2:13 Externally initialized: 43.97705078125ms
Script snippet #2:20 Externally declared: 35.217041015625ms
undefined
test(1000000)
Script snippet #2:6 Internally initialized: 12.23388671875ms
Script snippet #2:13 Externally initialized: 10.057861328125ms
Script snippet #2:20 Externally declared: 8.0849609375ms
undefined
test(100000)
Script snippet #2:6 Internally initialized: 0.94873046875ms
Script snippet #2:13 Externally initialized: 1.088134765625ms
Script snippet #2:20 Externally declared: 0.931884765625ms
undefined
test(10000)
Script snippet #2:6 Internally initialized: 0.1181640625ms
Script snippet #2:13 Externally initialized: 0.176025390625ms
Script snippet #2:20 Externally declared: 0.145751953125ms
undefined
test(1000)
Script snippet #2:6 Internally initialized: 0.044921875ms
Script snippet #2:13 Externally initialized: 0.04296875ms
Script snippet #2:20 Externally declared: 0.046875ms
undefined
test(100)
Script snippet #2:6 Internally initialized: 0.025146484375ms
Script snippet #2:13 Externally initialized: 0.033935546875ms
Script snippet #2:20 Externally declared: 0.02783203125ms
undefined
test(10)
Script snippet #2:6 Internally initialized: 0.024169921875ms
Script snippet #2:13 Externally initialized: 0.024169921875ms
Script snippet #2:20 Externally declared: 0.02197265625ms
undefined
test(1)
Script snippet #2:6 Internally initialized: 0.02490234375ms
Script snippet #2:13 Externally initialized: 0.032958984375ms
Script snippet #2:20 Externally declared: 0.030029296875ms
undefined

Firefox 54.0下的测试结果,没有考虑更多的测试条件、环境因素影响等等:

test(1000000000)
Internally initialized: 计时器开始
Internally initialized: 768.81ms
Externally initialized: 计时器开始
Externally initialized: 697.8ms
Externally declared: 计时器开始
Externally declared: 704.16ms
undefined
test(100000000)
Internally initialized: 计时器开始
Internally initialized: 131.84ms
Externally initialized: 计时器开始
Externally initialized: 84.42ms
Externally declared: 计时器开始
Externally declared: 70.98ms
undefined
test(10000000)
Internally initialized: 计时器开始
Internally initialized: 19.92ms
Externally initialized: 计时器开始
Externally initialized: 17.09ms
Externally declared: 计时器开始
Externally declared: 15.33ms
undefined
test(1000000)
Internally initialized: 计时器开始
Internally initialized: 3.46ms
Externally initialized: 计时器开始
Externally initialized: 3.06ms
Externally declared: 计时器开始
Externally declared: 2.75ms
undefined
test(100000)
Internally initialized: 计时器开始
Internally initialized: 1.49ms
Externally initialized: 计时器开始
Externally initialized: 1.11ms
Externally declared: 计时器开始
Externally declared: 2.14ms
undefined
test(10000)
Internally initialized: 计时器开始
Internally initialized: 1.03ms
Externally initialized: 计时器开始
Externally initialized: 1.11ms
Externally declared: 计时器开始
Externally declared: 1.11ms
undefined
test(1000)
Internally initialized: 计时器开始
Internally initialized: 1.25ms
Externally initialized: 计时器开始
Externally initialized: 0.84ms
Externally declared: 计时器开始
Externally declared: 0.82ms
undefined
test(100)
Internally initialized: 计时器开始
Internally initialized: 1.29ms
Externally initialized: 计时器开始
Externally initialized: 1.02ms
Externally declared: 计时器开始
Externally declared: 1.21ms
undefined
test(10)
Internally initialized: 计时器开始
Internally initialized: 1.29ms
Externally initialized: 计时器开始
Externally initialized: 1.1ms
Externally declared: 计时器开始
Externally declared: 1.64ms
undefined
test(1)
Internally initialized: 计时器开始
Internally initialized: 1.15ms
Externally initialized: 计时器开始
Externally initialized: 0.97ms
Externally declared: 计时器开始
Externally declared: 0.89ms
undefined

三种方式无明显差别。从一般性逻辑来分析,声明和初始化是两种操作,其中任何一种操作发生的次数越多耗时越多;但变量声明时就及时初始化是个好的编程习惯,未初始化的变量就直接参与运算常常会得到意想不到的结果,如(undefined) + 1 = NaN、(undefined) + 'string' = "undefinedstring",当然很多编程语言对于未初始化的变量以默认类型值初始化,而Javascript默认值为undefined。另外,这里不探讨存储性能和垃圾回收机制。

关于Javascript循环体变量声明与初始化的效率问题的更多相关文章

  1. JavaScript中变量声明有var和没var的区别

    JavaScript中变量声明有var和没var的区别 JavaScript中有var和没var的区别 Js中的变量声明的作用域是以函数为单位,所以我们经常见到避免全局变量污染的方法是 (functi ...

  2. javascript的变量声明、数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. JavaScript 基础 (变量声明, 数据类型, 控制语句)

    创建: 2017/09/16 更新: 2017/09/24 更改标题 [JavaScript 概要]-> [JavaScript 基础] 完成: 2017/09/25 更新: 2017/10/0 ...

  4. javascript的变量声明和数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. JavaScript中变量声明以及数据类型

    JavaScript变量 变量名必须以字母或下划线("_")开头 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做) 变量名称对大小写敏感(y 和 Y 是不同的变量) 变量可 ...

  6. 【C++】const,static和static const类型成员变量声明及其初始化

    1)const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间 void f1() { ; cout<<x<<endl; ...

  7. C++ static、const和static const类型成员变量声明以及初始化

    C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. sta ...

  8. javascript:变量声明&&赋值的提升和函数声明&&定义的提升在不同情况下的表现

    console.log(a); //undefined console.log(show); //函数的定义 show();         //aaa123 var a = 1; function ...

  9. JavaScript中变量声明效率问题

    1 var theString1 = "字符串1"; var theString2 = "字符串1"; var theString3 = "字符串1& ...

随机推荐

  1. MySQL存储过程_创建-调用

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的"脚本" 创建存储过程 调用存储过程 存储过程体 语句块标签 存储过程的参数 in:向过程里传参 out:过程向 ...

  2. Windows10 图标重建

    Windows10 图标重建 有没有遇到电脑上某个图标成了黑块,白块或者没有图呢. 那这样的话就可以使用图标重建啦. 删掉Windows10的图标文件如下图 路径: %userprofile%\App ...

  3. logback配置文件详解

    一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文 ...

  4. Java Socket应用---通信是这样练成的

    网络基础简介 Java 中网络相关 API 的应用     Java 中的 InetAddress 的应用   Test01.java package com.imooc; import java.n ...

  5. [刷题]算法竞赛入门经典(第2版) 4-2/UVa201 - Squares

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,20 ms) #include<iostream> #include<cs ...

  6. 策略模式(stragegy)

    策略模式(stragegy) 定义了算法族,分别封装起来,让它们之间可以相互替换,此模式让算法独立于使用算法的客户(Head First 设计模式).    策略模式,针对接口编程,而不依赖于具体的实 ...

  7. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  8. 基于OWIN+DotNetOpenOAuth实现OAuth2.0

    这几天时间一直在研究怎么实现自己的OAuth2服务器,对于太了解OAuth原理以及想自己从零开始实现的,我建议可以参考<Apress.Pro ASP.NET Web API Security&g ...

  9. Visual Studio2017中如何让Entity Framework工具【ADO.NET实体数据模型】支持MYSQL数据源

    熟悉Entity Framework应该对以下图片不陌生,他就是ADO.NET实体数据模型向导:可以将数据库的表自动生成模型类,或者创建Code First的模型文件. 但是这个模型向导默认只显示微软 ...

  10. How To Configure VMware fencing using fence_vmware_soap in RHEL High Availability Add On(RHEL Pacemaker中配置STONITH)

    本文主要简单介绍一下如何在RHEL 7 Pacemaker中配置一个fence_vmware_soap类型的STONITH设备(仅供测试学习). STONITH是Shoot-The-Other-Nod ...