关于Javascript循环体变量声明与初始化的效率问题

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

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。另外,这里不探讨存储性能和垃圾回收机制。

时间: 2024-11-09 10:17:04

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

javascript中变量声明要早于赋值

javascript中变量声明要早于赋值:这里只做简单的现象介绍,具体不做深究,先看一段代码实例: var webName="蚂蚁部落"; document.write(webName); 对于这段代码应该没有任何问题,输出结果是:蚂蚁部落.再来看一段代码: document.write(webName); var webName="蚂蚁部落"; 按照代码是顺序执行的理论,以上代码应该会报错才对,但是这里输出结果却是:undefined.这说明在执行输出之前,变量已

javascript的变量声明、数据类型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> /* javascript的变量声明:

4.PL_SQL——变量声明以及初始化,字符串分隔符

一.变量的作用 PL/SQL既然是编程语言,就必然会用到变量.和其他程序语言类似,PL/SQL中的变量主要有以下作用:1. 用来临时的存放数据:2.用来操作数据:3. 重复使用数据. 例如上一节中举的例子,就用到了变量: DECLARE v_fnameVARCHAR2(20); -- 声明变量 BEGIN SELECT first_name INTO v_fname -- 将SELECT语句查询到的数据存放到变量v_fname中 FROM employees WHERE employee_id

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

本文来论述JavaScript中变量声明有var和没var的区别,关于Js中的变量声明的作用域是以函数为单位,所以我们经常见到避免全局变量污染的方法是 (function(){ ... })(): 在函数内部,有var和没var声明的变量是不一样的.有var声明的是局部变量,没var的,声明的全局变量. 在全局作用域内声明变量时,有var 和没var看起来都一样,我们知道,声明的全局变量,就是window的属性,究竟是否一样,我们通过ECMAScrpit5提供的属性的特性查询方法,来发现之间的区

变量——声明,初始化,匿名变量,作用域

1.变量声明 1)标准格式: var 变量名 变量类型 当一个变量被声明之后,系统自动赋予它该类型的零值:int 为 0,float 为 0.0,bool 为 false,string 为空字符串,指针为 nil 等.所有的内存在 Go 中都是经过初始化的. 2)批量格式 var (变量名1 变量类型1变量名2 变量类型2变量名3 变量类型3) 3)简短格式 变量名 := 表达式 简短格式的限制:定义变量,同时显式初始化.不能提供数据类型.只能用在函数内部. 注意: 在单个短变量声明和赋值中,由

成员变量声明时初始化和构造方法中初始化的区别

声明时为成员变量赋值,那么你一创建对象,这个赋值就进行,而且先于构造器执行. 而且你每次创建这个类的对象,都是同一个值. 构造方法初始化可以单独为每一个对象赋不同的值 执行顺序:执行父类静态代码 执行子类静态代码初始化父类成员变量(我们常说的赋值语句)初始化父类构造函数初始化子类成员变量初始化子类构造函数

javascript变量声明提升(hoisting)

javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把所有变量的声明都提升到当前作用域的最前面. 先看一段代码 1 2 3 4 5 var v = "hello"; (function(){   console.log(v);   var v = "world"; })(); 这段代码运行的结果是什么呢? 答案是:undefined 这段代码说明了两个问题, 第一,function作用域里的变量v遮盖了上层作用域变量v.代

javascript变量声明提升

javascript变量声明提升(hoisting) javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把所有变量的声明都提升到当前作用域的最前面. 先看一段代码 1 2 3 4 5 var v = "hello"; (function(){   console.log(v);   var v = "world"; })(); 这段代码运行的结果是什么呢?答案是:undefined这段代码说明了两个问题,第一,funct

【转】javascript变量声明 及作用域

javascript变量声明提升(hoisting) javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把所有变量的声明都提升到当前作用域的最前面. 先看一段代码 1 2 3 4 5 var v = "hello"; (function(){   console.log(v);   var v = "world"; })(); 这段代码运行的结果是什么呢?答案是:undefined这段代码说明了两个问题,第一,funct