javascript - 为什么执行相同的 JavaScript 代码所花费的时间不同

标签 javascript google-chrome underscore.js

查看控制台,每次时间都不一样,我不知道为什么

我正在尝试执行一个函数并获取它所花费的时间,我使用了console.time(),但是每次运行时它都显示不同的时间 另一件事是,下划线所花费的时间是否超过正常执行时间。

function map(){
		
		console.time('mycode');
		
		var arr = [234345345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,5445645,456456,456456,234,467,234234234,456,43535,456,12,3123123123,456,456456,1231235,6456456,234,234456,4,5632452344,65,543452334124436,2345,345346,2345];
		var result = [];
		var constant = 3;
		var length = arr.length; //will avoid calculation of length each time
		for(i=0;i<length;i++){
			temp = arr[i] * constant;
			result.push(temp);
		}
		
		console.timeEnd('mycode');
		
		console.time('mycode1');
		
		var underscore = _.map([arr], function(num){ return num * 3; });
		
		console.timeEnd('mycode1');
	}
	
	function where(){
		
		console.time("myCode");
		var arr =  [{title: "Cymbeline", author: "Shakespeare", year: 1611},
			
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615},
			{title: "The Tempest", author: "Shakespeare", year: 1613},
			{title: "The Tempest", author: "Shakespeare", year: 1615}
		];
		var author = 'Shakespeare';
		var year = '1611';
		var result = [];
		var length = arr.length; //will avoid calculation of length each time
		for(i=0;i<length;i++){ //you know that and takes more time than or statement
			if(arr[i]['author'] == author && arr[i]['year'] == year){
				result.push(arr[i]);
			}
		}
		console.log(result)
		console.timeEnd("myCode");
		
		console.time("myCode1");
		var underscore = _.filter(arr, {author: "Shakespeare", year: 1611});
		
		console.log(underscore)
		console.timeEnd("myCode1");
	}
.container{
		width:calc(100% - 200px);
		text-align:center;
		margin:0 auto;
		background:#f4f4f4;
	}
<script src="http://underscorejs.org/underscore-min.js"></script>

<h1>Evaluating time </h1>
<input type='button' onclick='map()' value='Execute'>
<input type='button' onclick='where()' value='Execute'>
  

最佳答案

执行代码所需的时间会因 CPU 正在执行的其他工作而异。由于计算机上正在运行其他进程,中断不断发生。您的 Web 应用程序只是需要一定 CPU 时间的众多应用程序之一。还有一些因素,例如 CPU 对频繁请求的数据执行的缓存以及操作系统缓存。

关于javascript - 为什么执行相同的 JavaScript 代码所花费的时间不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37986905/

相关文章:

javascript - meteor 用户帐户和 alanning meteor 角色 - 检查登录时的角色

javascript - JS 比较两个对象数组,并排除匹配值的元素到新数组中

node.js - 如何在underscore.js中使用对象 '_'

javascript - 根据子元素 href 属性将事件类设置为元素

javascript 意外标记。 `{}.toString()`

javascript - 如何在不同的 JavaScript/Parse 对象之间共享方法?

javascript - Chrome 扩展中的跨源 XMLHttpRequest

javascript - Google Chrome 扩展程序的 storage.sync 上出现 QUOTA_BYTES_PER_ITEM 错误

javascript - CSS float Chrome 与 Firefox

Backbone.js-选中复选框的最佳方法是什么