toArray vs slice
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var slice = Array.prototype.slice;
function toArray(obj) {
var arr = [];
for (var i = 0, len = obj.length; i < len; i++) {
arr[i] = obj[i];
}
return arr;
}
var test_arr1 = [];
var test_arr2 = [];
var test_arr3 = [];
for (var i = 0; i < 10; i++) {
test_arr1[i] = i;
}
for (var i = 0; i < 1000; i++) {
test_arr2[i] = i;
}
for (var i = 0; i < 1000000; i++) {
test_arr3[i] = i;
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
slice small |
|
pending… |
toArray small |
|
pending… |
slice medium |
|
pending… |
toArray medium |
|
pending… |
slice large |
|
pending… |
toArray large |
|
pending… |
slice small single |
|
pending… |
toArray small single |
|
pending… |
slice medium single |
|
pending… |
toArray medium single |
|
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
0 comments