关于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& ...
随机推荐
- Python中使用with语句同时打开多个文件
下午小伙伴问了一个有趣的问题, 怎么用 Python 的 with 语句同时打开多个文件? 首先, Python 本身是支持同时在 with 中打开多个文件的 with open('a.txt', ' ...
- 蓝桥杯-马虎的算式-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- SSH小结
工作有一段时间了,经常用SSH登录远程机器,但对原理一直不是很了解,所以查阅了一些资料,写个小结. 一. SSH是什么? SSH的全称是Secure Shell, 是一种"用来在不安全的网络 ...
- vue视频学习笔记07
video 7 vue问题:论坛http://bbs.zhinengshe.com------------------------------------------------UI组件别人提供好一堆 ...
- String深度解析
文章出处:http://my.oschina.net/xiaohui249/blog/170013 一.引题 String类型是比较特殊的一种类型,同时也是面试经常被问到的一个知识点,本文结合java ...
- 建造者(Builder)模式
建造者模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal representation)与产品的生产过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象. 产品的 ...
- 【JAVAWEB学习笔记】04_JavaScript
晨读单词: onmouseover:鼠标移入 onmouseout:鼠标移出 attribute:属性 node:节点 document:文档 element:元素 textNode:文本节点 app ...
- PHP. 01. C/S架构、B/S架构、服务器类型、服务器软件、HTTP协议/服务器、数据库、服务器web开发、PHP简介/常见语法、PHPheader()、 PHP_POST/GET数据获取和错误处理
C/S架构 Client/Server 指客户端,服务器 架构的意思 优点:性能性高:可将一部分的计算工作放在客户端上,服务器只需处理出局即可 洁面炫酷,可使用更多系统提供的效果 缺点:更新软件需 ...
- ztree实现权限功能(横向显示)
最近在做权限功能的时候,采用的ztree实现的,但是产品要求最后一层的权限节点要横向显示.开始在网上找的解决方案是用css样式把最后一层的display设置为inline.在我本地电脑上看了下.效果不 ...
- WINFORM实现进程信息的查看,listview,点击,右键,右键菜单
1. 程序设计要求 设计程序来获取计算机中的进程信息 2.程序设计流程图 3.程序设计亮点 (0)程序启动时加载guide引导使用视频 (1)使用Listview控件显示出所有控件的名称. (2) ...