Javascript for inとfor性能比較

4226 ワード

直接コード
VM229:10 1:  for     
VM229:15 2:   for in   
  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM229:10 1: 1.197021484375ms
VM229:15 2: 2.094970703125ms
undefined
  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM232:10 1: 1.16015625ms
VM232:15 2: 1.801025390625ms
undefined
  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM234:10 1: 0.8798828125ms
VM234:15 2: 1.885009765625ms
undefined
  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM236:10 1: 0.873779296875ms
VM236:15 2: 1.7890625ms

  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM238:10 1: 1.22998046875ms
VM238:15 2: 1.938720703125ms

  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM240:10 1: 0.932861328125ms
VM240:15 2: 1.74609375ms

  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM242:10 1: 1.039794921875ms
VM242:15 2: 2.253173828125ms

  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM244:10 1: 0.869140625ms
VM244:15 2: 1.6669921875ms

  var a = [];
        var a2 = []
        for (var i = 0; i < 10000; i++) {
            a.push(i)
        }
        console.time("1")
        for (var g = 0; g < a.length; g++) {
            a2.push(a[g])
        }
        console.timeEnd("1")
        console.time("2")
        for (var x in a) {
            a2.push(a[x])
        }
        console.timeEnd("2")
VM246:10 1: 1.195068359375ms
VM246:15 2: 2.1611328125ms
まだfor性能がやや早いようです.