Javascript 性能循环,是否使用该属性
我对性能非常感兴趣,我想知道在这种情况下应该如何使用 这: 或者这个: var映射器Javascript 性能循环,是否使用该属性,javascript,performance,loops,Javascript,Performance,Loops,我对性能非常感兴趣,我想知道在这种情况下应该如何使用 这: 或者这个: var映射器 Mapper = function() { this.files = this.files; this.result = []; return this.each_file(); }; Mapper.prototype.each_file = function() { var index, length; length = this.files.length; index = 0;
Mapper = function() {
this.files = this.files;
this.result = [];
return this.each_file();
};
Mapper.prototype.each_file = function() {
var index, length;
length = this.files.length;
index = 0;
while (index < length) {
this.result.push(this.get_info(this.file[index]));
index++;
}
return this.result;
};
Mapper.prototype.get_info = function(file) {
var info;
return info = {
name: file,
exist: null,
status: [],
type: 'pdf'
};
};
module.exports = Mapper;
解释:
在第一段代码中,我在所有属性中使用它,在第二段代码中,我在一些属性中使用它。我想知道哪一个更聪明,更好地解决性能问题
OBS:我正在循环超过3000个文件。从性能角度看,我认为您不会看到任何差异。我想说,由于对象属性是如何找到的,选项2可能会更快,但是这个更快的速度太小了,您甚至可能不担心它。
但是从代码风格/最佳实践的角度来看,选项2更好如果您询问使用此变量或局部函数范围内的变量是否对性能有任何显著影响,那么答案是否定的。但是,长度和索引实际上是属于局部函数范围的时间变量,因此,您的第二个实现更有意义。3000次不足以注意到两个版本之间的细微差异。您可以使用类似的工具进行此类测试。但在这种情况下,这种差异将不会真正消失,我认为这与性能无关。但是在实例中存储helper变量似乎是错误的。我会用第二个。
Mapper = function() {
this.files = this.files;
this.result = [];
return this.each_file();
};
Mapper.prototype.each_file = function() {
var index, length;
length = this.files.length;
index = 0;
while (index < length) {
this.result.push(this.get_info(this.file[index]));
index++;
}
return this.result;
};
Mapper.prototype.get_info = function(file) {
var info;
return info = {
name: file,
exist: null,
status: [],
type: 'pdf'
};
};
module.exports = Mapper;