找到你要的答案

Q:Performance looping, using this in which attribute or not

Q:绩效循环,用这个属性或不

i'm very interest in performance, and i would like to know how should i use in this case.

This:

var Mapper;

Mapper = function() {
  this.files = this.files;
  this.result = [];
  return this.each_file();
};

Mapper.prototype.each_file = function() {
  this.length = this.files.length;
  this.index = 0;
  while (this.index < this.length) {
    this.result.push(this.get_info(this.file[this.index]));
    this.index++;
  }
  return this.result;
};

Mapper.prototype.get_info = function(file) {
  var info;
  return info = {
    name: file,
    exist: null,
    status: [],
    type: 'pdf'
  };
};

module.exports = Mapper;

Or this: var Mapper;

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;

Explaining:

I am doing a while looping to get information from files, in the first code, i'm using this in ALL properties, and in the second one i'm using this in just a few. I would like to know which one is the smarter, better for performance issues.

OBS: I'm looping for more than 3.000 files.

我很感兴趣的性能,我想知道我应该如何使用这种情况下。

这:

var Mapper;

Mapper = function() {
  this.files = this.files;
  this.result = [];
  return this.each_file();
};

Mapper.prototype.each_file = function() {
  this.length = this.files.length;
  this.index = 0;
  while (this.index < this.length) {
    this.result.push(this.get_info(this.file[this.index]));
    this.index++;
  }
  return this.result;
};

Mapper.prototype.get_info = function(file) {
  var info;
  return info = {
    name: file,
    exist: null,
    status: [],
    type: 'pdf'
  };
};

module.exports = Mapper;

Or this: var Mapper;

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:我循环超过3文件。

answer1: 回答1:

If you are asking whether using variable from this or variable in the local function scope has any significant impact on performance then the answer is no. However, length and indices are really temporal variables that belongs to local function scope, so your second implementation makes more sense.

如果你问在本地函数范围内的变量或变量是否对性能有任何重大影响,那么答案是否定的。但是,长度和索引实际上是属于局部函数作用域的时间变量,所以你的第二个实现更有意义。

answer2: 回答2:

Well from performance point of view I don't think you will see any difference. I would say that option 2 is potentially faster due to the fact how object property gets found, but again this "faster" is so small that you may even not wary about it. But from code style/best practices point of view option 2 is better

Well from performance point of view I don't think you will see any difference. I would say that option 2 is potentially faster due to the fact how object property gets found, but again this "faster" is so small that you may even not wary about it. But from code style/best practices point of view option 2 is better

javascript  performance  loops