关于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& ...
随机推荐
- ASP.NET WebForm
ASP.NET WEBFORM Aspx aspx文件(新建项→Web窗体) .在aspx中可以使用<%=表达式%>的方式在页面的当前位置输出表达式(表达式至少要用protected级别, ...
- scp 命令快速使用讲解
在 Linux 下使用 scp 命令 scp 是安全拷贝协议(Secure Copy Protocol)的缩写,和众多 Linux/Unix 使用者所熟知的拷贝(cp)命令一样.scp 的使用方式类似 ...
- STL容器之优先队列(转)
STL容器之优先队列 原地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关 ...
- 观察者模式(Observer)发布、订阅模式
观察者模式定义了对象之间一对多的依赖关系,这样一来,当一个对象改变时,他的所有依赖者都会收到通知并自动更新. 模式中的角色 1.抽象主题(Subject):它把所有观察者对象的引用保存到一个聚集里 ...
- Nginx 反向代理&负载均衡
1.反向代理 当我们请求一个网站时,nginx会决定由哪台服务器提供服务,就是反向代理. nginx只做请求的转发,后台有多个tomcat服务器提供服务,nginx的功能就是把请求转发给后面的服务器, ...
- 如何使用angularJs
本期更新,博主将给大家分享一些AngularJs常用的一些属性和方法,AngularJS 是由 Google 的员工 Miško Hevery 从 2009 年开始着手开发.这是一个非常好的构想,该项 ...
- 由于losf引起的pxc启动报错处理
PXC主节点启动完成后,再启动node1,error日志报错: 2017-05-02T15:23:42.830888Z 0 [ERROR] WSREP: Failed to read 'ready & ...
- 微软 Build 2017 开发者大会:Azure 与 AI 的快速发展
欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 一年一度的微软 Build 大会准时起航,本年度大会从旧金山移师西雅图,一个近年来凭借女神汤唯而在中国家喻户晓的美国西部海滨城市 ...
- 学习MVC之租房网站(九)-房源显示和搜索
在上一篇<学习MVC之租房网站(八)- 前台注册和登录>完成了前台用户的注册.登录.重置密码等功能,然后要实现与业务相关的功能,包括房源的显示.检索等. 一 房源显示 房源显示内容较多,涉 ...
- Django项目部署到Apache服务器
本文讲述的是在阿里云服务器(ECS)上部署Django项目于Apache,服务器操作系统为ubuntu,公网Ip地址为123.56.30.151. 将Django部署到Apache服务器的原因 Dja ...