Empty JavaScript array redux
JavaScript performance comparison
Info
Original discussion at http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript.
This version of the test is much slower because it needs to copy the pre-populated array in each iteration. I have included a control snippet to calculate the time needed for the copy.
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 | |
---|---|---|
control (small) |
|
pending… |
control (medium) |
|
pending… |
control (big) |
|
pending… |
new array (small) |
|
pending… |
new array (medium) |
|
pending… |
new array (big) |
|
pending… |
length = 0 (small) |
|
pending… |
length = 0 (medium) |
|
pending… |
length = 0 (big) |
|
pending… |
splice (small) |
|
pending… |
splice (medium) |
|
pending… |
splice (big) |
|
pending… |
while;pop (small) |
|
pending… |
while;pop (medium) |
|
pending… |
while;pop (big) |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
0 comments
Comment form temporarily disabled.