Array cloning
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
function cloneObject(object) {
var clone = new object.constructor;
for (var key in object) {
clone[key] = object[key];
}
return clone;
}
function cloneArray1(array) {
var clone = [];
for (var i = 0; i < array.length; i++) {
clone[i] = array[i];
}
return clone;
}
function cloneArray2(array) {
var clone = [],
len = array.length;
for (var i = 0; i < len; i++) {
clone[i] = array[i];
}
return clone;
}
function cloneArray3(array) {
return array.concat();
}
function cloneArray4(array) {
return array.slice();
}
function cloneArray5(array) {
return array.slice(0);
}
function cloneArray6(array) {
return array.filter(True);
}
function cloneArray7(array) {
return array.map(Identity);
}
function cloneArray8(array) {
return Array.apply([], array);
}
function cloneArray9(array) {
return array.splice(0);
}
function cloneArray10(array) {
return [].concat(array);
}
function True() {
return true;
}
function Identity(v) {
return v;
}
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
for-in |
|
pending… |
for |
|
pending… |
for (cached) |
|
pending… |
concat() |
|
pending… |
slice() |
|
pending… |
slice(0) |
|
pending… |
filter(True) |
|
pending… |
map(Identity) |
|
pending… |
Array.apply([], array) |
|
pending… |
splice(0) |
|
pending… |
concat new array |
|
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 Athorcis
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 5: published
0 comments