make filled array
JavaScript performance comparison
Info
Compare speeds of native array methods vs a while loop
Preparation code
<script>
Benchmark.prototype.setup = function() {
function spliceMap(length, fill){
var a = [], b = [];
a.length = length + 2;
a[0] = 0; a[1] = 0;
b.splice.apply(b,a);
return b.map(function(){return fill;});
}
function pushMap(length, fill){
var a = [], b = [];
a.length = length;
b.push.apply(b,a);
return b.map(function(){return fill;});
}
function whileLengthNew(len, val) {
var rv = new Array(len);
while (--len >= 0) {
rv[len] = val;
}
return rv;
}
function whileLengthLiteral(len, val) {
var rv = [];
while (--len >= 0) {
rv[len] = val;
}
return rv;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
spliceMap |
|
pending… |
pushMap |
|
pending… |
whileLengthLiteral |
|
pending… |
whileLengthNew |
|
pending… |
BIG pushMap |
|
pending… |
BIG whileLengthNew |
|
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
- Revision 2: published
- Revision 3: published by Göran Andersson
0 comments