Converting arguments to an array
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var Array = window.Array;
var push = Array.prototype.push;
var slice = Array.prototype.slice;
var unshift = Array.prototype.unshift;
function arraySlice(array) {
return slice.call(array);
}
function arrayPush(array) {
var x = [];
push.apply(x, array);
return x;
}
function arrayUnshift(array) {
var x = [];
unshift.apply(x, array);
return x;
}
function whileLoop(array) {
var i = -1,
l = array.length,
x = Array(l);
while (++i < l) {
x[i] = array[i];
}
return x;
}
function byConstructor(array) {
return Array.apply(null, array);
}
var largeArray = Array(1001).join("a").split("");
var mediumArray = Array(101).join("a").split("");
var smallArray = Array(6).join("a").split("");
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Array#slice |
|
pending… |
Array#push |
|
pending… |
Array#unshift |
|
pending… |
Array |
|
pending… |
while loop |
|
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
- Revision 4: published by tj
- Revision 5: published
- Revision 6: published
- Revision 7: published by damien maillard and last updated
- Revision 8: published by rektide
- Revision 9: published
- Revision 11: published by Charmander
- Revision 14: published
0 comments