我目前的功能大约可以运行200次。
功能如下:
function GetB(av,bol){
var bxes=[
["11","12","13","21","22","23","31","32","33"],
["14","15","16","24","25","26","34","35","36"],
["17","18","19","27","28","29","37","38","39"],
["41","42","43","51","52","53","61","62","63"],
["44","45","46","54","55","56","64","65","66"],
["47","48","49","57","58","59","67","68","69"],
["71","72","73","81","82","83","91","92","93"],
["74","75","76","84","85","86","94","95","96"],
["77","78","79","87","88","89","97","98","99"]
];
//code
}
我的第一个问题是这个数组正在减慢一切,因为我认为每次数组bxes
(或类似的东西)都会重写
此bxes
数组永远不会被修改,我不介意将其设为全局数据。
答案 0 :(得分:3)
为什么不会你将它移到函数之外? (为什么冒风险导致性能问题?)
它不一定必须是“全局的” - 只是在函数的父作用域中 - 但函数和bxes
以及其他代码都可以存在于父函数或闭包中...... < / p>
(function(){
var bxes = [...];
window.GetB = function(av,bol){...};
})();
答案 1 :(得分:3)
简单的基准测试显示了答案。使用您编写的函数声明,5,000,000次调用需要12.739秒。将数组定义移到函数外部,同一个循环只消耗0.169。
请记住,这将根据JavaScript引擎 - 以及浏览器 - 的使用情况而有所不同。