关于Javascript循环体变量声明与初始化的效率问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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。另外,这里不探讨存储性能和垃圾回收机制。
以上是关于关于Javascript循环体变量声明与初始化的效率问题的主要内容,如果未能解决你的问题,请参考以下文章
缺少javascript;在 for 循环初始化程序之后(来源不明