Javascript 中什么更有效:while 或 for 语句? [英] What is more efficient in Javascript : while or for statements?

查看:47
本文介绍了Javascript 中什么更有效:while 或 for 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近有机会在 javascript 中进行循环,我很困惑是使用 for 循环还是 while 语句.

I recently got the opportunity to do looping in javascript and I was rather confused whether to use for loop or while statement.

 var i = foo.length;
 while(i--){

 }

 for(var i=0 ;i<=foo.length;i++){

 }

我想从 javascript 人员那里知道哪个更有效,我们应该在什么情况下相应地使用它们.是和java中的原因相同还是不同的原因.

I want to know from javascript guys which one is more effecient to use and under what circumstances should we use them accordingly. Is it same reason as in java or someting different.

推荐答案

理论上 while 循环更快,因为 for 循环查找 length<每次循环时 foo 的/code> 属性,但在实际使用中它会产生不可估量的微小差异.

In theory the while loop is quicker because the for loop looks up the length attribute of foo every time though the loop, but in real-world use it's going to make an immeasurably small difference.

这篇关于Javascript 中什么更有效:while 或 for 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆