array cloning

JavaScript performance comparison

Revision 11 of this test case created by Anton Kudris

Info

speed comparissons between slice, splice, for, $.clone

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var a = ['z','a','q','w','s','x','c','d','v','e','f','t','r','g','t','m','y','h','t','y','j','i','o','l','f','s','d','j','n','m','c','u','i','e','w','h','r',',','w','e','n','r','w','n','e','b','f','2','3','4','2','3','4','n','j','m','f','d','s','j','d','y','f','g','2','3','h','f','s','j','d','b','m','f','h','s','d','h','f','o','2','3','i','z','a','q','w','s','x','c','d','v','e','f','t','r','g','t','m','y','h','t','y','j','i','o','l','f','s','d','j','n','m','c','u','i','e','w','h','r',',','w','e','n','r','w','n','e','b','f','2','3','4','2','3','4','n','j','m','f','d','s','j','d','y','f','g','2','3','h','f','s','j','d','b','m','f','h','s','d','h','f','o','2','3','i','z','a','q','w','s','x','c','d','v','e','f','t','r','g','t','m','y','h','t','y','j','i','o','l','f','s','d','j','n','m','c','u','i','e','w','h','r',',','w','e','n','r','w','n','e','b','f','2','3','4','2','3','4','n','j','m','f','d','s','j','d','y','f','g','2','3','h','f','s','j','d','b','m','f','h','s','d','h','f','o','2','3','i'];
</script>
<script>
Benchmark.prototype.setup = function() {
    var b = [];
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Slice
b = a.slice(0, a.length);
pending…
concat
b = [].concat(a);
pending…
Splice
b = a.splice(0, a.length);
pending…
For
b = [];
for (var i = a.length-1; i >= 0; i--) {
    b.unshift(a[i]);
}
pending…
bad for
b = [];
for (var i = 0; i < a.length; i++) {
    b.push(a[i]);
}
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:

0 comments

Add a comment