Empty JavaScript array
JavaScript performance comparison
Info
Original discussion at http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var small = [],
medium = [],
big = [],
smallSize = 100,
mediumSize = 10000,
bigSize = 100000;
while (smallSize--) {
small.push(Math.random());
}
while (mediumSize--) {
medium.push(Math.random());
}
while (bigSize--) {
big.push(Math.random());
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
Overwriting with a new array (small) |
|
pending… |
Overwriting with a new array (medium) |
|
pending… |
Overwriting with a new array (big) |
|
pending… |
Setting length to 0 (small) |
|
pending… |
Setting length to 0 (medium) |
|
pending… |
Setting length to 0 (big) |
|
pending… |
Using splice (small) |
|
pending… |
Using splice (medium) |
|
pending… |
Using splice (big) |
|
pending… |
Using while, pop (small) |
|
pending… |
Using while, pop (medium) |
|
pending… |
Using while, pop (big) |
|
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 Gajus
- Revision 2: published
- Revision 3: published
- Revision 8: published
- Revision 9: published by serjout
- Revision 10: published by serjout
- Revision 11: published by serjout
- Revision 12: published
- Revision 13: published
- Revision 14: published
- Revision 15: published
0 comments