forloop vs reverse while loop and array reverse
JavaScript performance comparison
Info
I know that reverse while looping in javascript is faster than for looping, but if order is to be maintained, would rev-while looping then reversing the array be faster or would the traditional for loop out perform this combo?
See http://jsperf.com/js-array-reverse-vs-while-loop/5.
See http://jsperf.com/array-push-reverse-vs-unshift-reverse for array push reverse vs. unshift reverse.
See http://jsperf.com/array-push-vs-unshift/6 for array push vs. unshift reversal.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var length = array.length;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
for |
|
pending… |
for reverse |
|
pending… |
while |
|
pending… |
while reverse |
|
pending… |
reverse |
|
pending… |
push |
|
pending… |
unshift |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Dan Masquelier
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 5: published
- Revision 6: published
- Revision 7: published
- Revision 8: published by vamp
- Revision 9: published by XP1
- Revision 10: published by XP1
0 comments