循环使用较少属性的JavaScript对象会更快吗?

时间:2017-12-13 04:00:29

标签: javascript

如果有两个对象定义,其中一个对象具有比另一个更多的属性 -

示例对象:

var personObjectType1 = 
       {
          name: [a string],
          age: [an int]
       };

var personObjectType2 = 
       {
          name: [a string],
          age: [an int],
          address: [a string],
          photo: [a link],
          spouse: [a string],
          cars: [an array of strings],
          children: [an array of strings],
          pets: [an array of strings],
          father: [a string],
          mother: [a string],
          birthdate: [a date]    
       };

并且每个对象都有一个相等长度的数组,对于具有较少属性的对象,循环遍历对象数组会更快吗?

(注意:对每种类型的对象执行相同的操作)

示例代码:

//personObjectType1Array is an array of 10000 personObjectType1's
//personObjectType2Array is an array of 10000 personObjectType2's

for (var i = 0; i < personObjectType1Array.length; i++)
{
    console.log(personObjectType1Array[i].age); 
}

for (var j = 0; j < personObjectType2Array.length; j++)
{
    console.log(personObjectType2Array[i].age); 
}

一个循环会比另一个循环运行得更快吗?为什么或为什么不呢?

修改 回复说没有区别,有谁能说出原因?

3 个答案:

答案 0 :(得分:1)

阵列

的性能几乎相同

&#13;
&#13;
var personObjectType1Array = [];
var personObjectType2Array = [];

for(var i=0; i<10000; i++)
  {
    personObjectType1Array.push({
          name: '[a string]',
          age: 25
       });
    
    personObjectType2Array.push(
      {
          name: '[a string]',
          age: 25,
          address: '[a string]',
          photo: '[a link]',
          spouse: '[a string]',
          cars: '[an array of strings]',
          children: '[an array of strings]',
          pets: '[an array of strings]',
          father: '[a string]',
          mother: '[a string]',
          birthdate: '[a date]'    
       }
    );
  }


//personObjectType1Array is an array of 10000 personObjectType1's
//personObjectType2Array is an array of 10000 personObjectType2's

var startTimeArray1 = window.performance.now();

for (var i = 0; i < personObjectType1Array.length; i++)
{
    //console.log(personObjectType1Array[i].age); 
}

console.log('TimeArray1 : ' + (window.performance.now() - startTimeArray1));

            
var startTimeArray2 = window.performance.now();

for (var j = 0; j < personObjectType2Array.length; j++)
{
    //console.log(personObjectType2Array[i].age); 
}

console.log('TimeArray2 : ' + (window.performance.now() - startTimeArray2));
&#13;
&#13;
&#13; 在这里,我创建了一个演示

https://jsbin.com/hurewod/edit?js,console

答案 1 :(得分:1)

你可以在console中查看两者的时间。拥有更多属性的人的执行时间会更多。

&#13;
&#13;
var personObjectType1 = 
       {
          name : 'xyz',
          age : 12,
          roll : 23,
          code :29,
          height :26,
          address:{"streetNo":121,"City":"New Delhi"}
       };

var personObjectType2 = {name : 'xyz'};
	   
	var t0 = performance.now();
        for (var i = 0; i < personObjectType1.length; i++){
         //console.log(personObjectType1[i].name); 
      }
     var t1 = performance.now();
     console.log("Call to personObjectType1 took " + (t1 - t0) + " milliseconds.")
     var t2 = performance.now();
     for (var j = 0; j < personObjectType2.length; j++){
       //console.log(personObjectType2[j].name); 
     }
      var t3 = performance.now();
     console.log("Call to personObjectType2 took " + (t3 - t2) + " milliseconds.")
&#13;
&#13;
&#13;

答案 2 :(得分:0)

可能是因为代码需要第一次编译。通过先调用所有方法,然后执行,您将获得最佳指标。 顺便说一句,循环之间的性能差异可以忽略不计,使用循环的可读性优势具有边际性能优势。