JS: Array.reverse() vs. for and while loops
JavaScript performance comparison
Info
Checking performance of Array.reverse() vs custom loops.
Also see http://jsperf.com/forloop-vs-reverse-while-loop-and-array-reverse/9.
Preparation code
<script>
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 | |
|---|---|---|
Array.reverse() |
|
pending… |
while push then slice |
|
pending… |
while forward |
|
pending… |
for push then slice |
|
pending… |
for push |
|
pending… |
for swap |
|
pending… |
for swap half |
|
pending… |
XOR swap |
|
pending… |
XOR swap half |
|
pending… |
destructuring |
|
pending… |
destructuring half |
|
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 Matt McDonald
- Revision 2: published
- Revision 3: published by Matt McDonald
- Revision 4: published by george
- Revision 5: published by XP1
- Revision 6: published by tav
- Revision 8: published by Miller Medeiros
- Revision 9: published by Boris
2 comments
Wow...just wow :)
The benchmark is just buggy. The "for push then slice" test modifies "length", so all the tests after that don't do anything useful.