关于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中变量声明有var和没var的区别
JavaScript中变量声明有var和没var的区别 JavaScript中有var和没var的区别 Js中的变量声明的作用域是以函数为单位,所以我们经常见到避免全局变量污染的方法是 (functi ...
- javascript的变量声明、数据类型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript 基础 (变量声明, 数据类型, 控制语句)
创建: 2017/09/16 更新: 2017/09/24 更改标题 [JavaScript 概要]-> [JavaScript 基础] 完成: 2017/09/25 更新: 2017/10/0 ...
- javascript的变量声明和数据类型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript中变量声明以及数据类型
JavaScript变量 变量名必须以字母或下划线("_")开头 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做) 变量名称对大小写敏感(y 和 Y 是不同的变量) 变量可 ...
- 【C++】const,static和static const类型成员变量声明及其初始化
1)const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间 void f1() { ; cout<<x<<endl; ...
- C++ static、const和static const类型成员变量声明以及初始化
C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. sta ...
- javascript:变量声明&&赋值的提升和函数声明&&定义的提升在不同情况下的表现
console.log(a); //undefined console.log(show); //函数的定义 show(); //aaa123 var a = 1; function ...
- JavaScript中变量声明效率问题
1 var theString1 = "字符串1"; var theString2 = "字符串1"; var theString3 = "字符串1& ...
随机推荐
- jquery获取文件名称
$("#fileupload").on("change",function(){ var filePath=$(this).val(); if(filePath ...
- java基础---->hashMap的简单分析(一)
HashMap是一种十分常用的数据结构对象,可以保存键值对.它在项目中用的比较多,今天我们就来学习一下关于它的知识. HashMap的简单使用 一.hashMap的put和get方法 Map<S ...
- hdu698 Just a Hook 线段树-成段更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 很简单的一个线段树的题目,每次更新采用lazy思想,这里我采用了增加一个变量z,z不等于0时其绝 ...
- jQuery 一个你意想不到的代码神器!
jQuery 一个你意想不到的代码神器! jQuery 选择器.(最简单,最基本) jquery选择器的优势: (1) 简洁的写法,$()函数 (2)支持CSS1到CSS3选择器 (3)完善的处理机制 ...
- winXP/win7/win10系统关闭445端口方法全攻略
近日有多个高校发布了关于连接校园网的电脑大面积中勒索病毒的消息,这种病毒致使许多高校毕业生的毕业论文(设计)被锁.受害机器的磁盘文件会被篡改为相应的后缀,图片.文档.视频.压缩包等各类资料都无法正常打 ...
- ConcurrentHashMap源码解析
转自:http://www.iteye.com/topic/344876 ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现. 实现原理 锁分离 (Lo ...
- elasticsearch系列(三)分表分库
首先ES没有库和表的概念,只有index,type,document(详细术语可以看ES的系列一 http://www.cnblogs.com/ulysses-you/p/6736926.html), ...
- 深入理解Java虚拟机 自己编译JDK
获取JDK源码 先明确OpenJDK和Sun/OracleJDK之间,以及OpenJDK 6.OpenJDK 7.OpenJDK7u和OpenJDK 8等项目之间是什么关系,这有助于确定接下来编译要使 ...
- 解决window.navigator.geolocation.getCurrentPosition在IOS10系统中无法进行地理定位问题
昨天接到用户通知说在点击"看场地"时无法获取地理位置信息. 在接到通知时,首先想到的是排查机型问题.由于客户多为IOS用户,所以最先看的是在安卓是有没有此问题的发生,调查结果为安卓 ...
- 【JAVAWEB学习笔记】20_增删改查
今天主要是利用三层架构操作数据库进行增删查改操作. 主要是编写代码为主. 附图: 前台和后台 商品的展示 修改商品