对于循环的优化

2019-10-15  本文已影响0人  AAA前端
var item = [0, 1, 2, 3, 4, 5]

for (var i = 0; i < item.length; i++) {
  // do something
}

var j = 0;
while (j < item.length) {
  // do something
}

var k = 0;
do {
  // do something
} while (k < item.length)

var item = [0, 1, 2, 3, 4, 5]

for (var i = 0, len = item.length; i < len; i++) {
  // do something
}

var j = 0,
  count = item.length;
while (j < count) {
  // do something
}

var k = 0,
  num = item.length;
do {
  // do something
} while (k < num)

var item = [0, 1, 2, 3, 4, 5]

for (var i = item.length; i--;) {
  // do something
}

var j = item.length;
while (j--) {
  // do something
}

var k = item.length;
do {
  // do something
} while (k--)

参考: 高性能 javaScript

上一篇下一篇

猜你喜欢

热点阅读