In these benchmarks, http://jsperf.com/the-loops , Barbara Cassani showed that a "reverse while" loop is way faster,
while (iterations > 0) {
a = a + 1;
a = a - 1;
iterations--;
}
than a usual "for" loop:
for (i = 0; i < iterations; i++) {
a = a + 1;
a = a - 1;
}
Why?
Update
Okay, forget about it, there is a bug in the test, iterations = 100
, is executed only once per page. Therefore reducing it, well, means that we don't really enter the loops. Sorry.
Except for the big bug in the initial test, here are the results:
for
vs while
makes no difference >
or <
are better than !==
It is because of of specifics of internals of each JavaScript engine. Don't use it for optimization, because you can't logically count on it always be faster as engines change. For example, check out last revision of test you've linked and note that difference is much more smaller if exists at all on recent browsers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.