Array Zero Fill
JavaScript performance comparison
Preparation code
<script>
function t1(len) {
var arr = new Array(len);
while (len-- > 0) {
arr[len] = 0;
}
}
function t2(len) {
var arr = new Array();
while (len-- > 0) {
arr[len] = 0;
}
}
function t3(len) {
var arr = new Array(len);
for (var i = 0; i < len; i++) {
arr[i] = 0;
}
}
function t4(len) {
var arr = new Array();
for (var i = 0; i < len; i++) {
arr[i] = 0;
}
}
function t5(len) {
var arr = new Array(len);
for (var i = len; i > 0; i--) {
arr[i] = 0;
}
}
function t6(len) {
var arr = new Array();
for (var i = len; i > 0; i--) {
arr[i] = 0;
}
}
function push(len)
{
var zeros = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var l = zeros.length,
r = len - l; // remainder
for (var i=r; i--;) {
zeros.push(0);
}
}
</script>
<script>
Benchmark.prototype.setup = function() {
var numzeros = 1000;
// start from (arbitrarily chosen) sixteen zeros
var zeros = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
t1 |
|
pending… |
t2 |
|
pending… |
t3 |
|
pending… |
t4 |
|
pending… |
t5 |
|
pending… |
t6 |
|
pending… |
SLICE + PUSH |
|
pending… |
PUSH FUNCTION |
|
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 Jonathan Larson
- Revision 2: published by Jonathan Larson
- Revision 4: published by PlayMyCode
- Revision 5: published
- Revision 6: published
- Revision 8: published by Timo
- Revision 9: published by Timo
- Revision 10: published by Timo
- Revision 11: published
- Revision 13: published
0 comments