Forgot to make sure that oldData actually had data in it before the copy attempt.
[jquery.git] / speed / slice.vs.concat.html
1 <script type="text/javascript">
2 (function(){
3         // Conclusions:
4         // slice() is a little faster than concat() except on Chrome
5         // This clone() is slower on FF & IE but takes 50% on Safari & Chrome
6
7         var SIZE = 1e4,
8                 LOOPS = 500;
9         
10         var arr = new Array(SIZE);
11         for ( var i=arr.length-1; i >= 0; --i )
12                 arr[i] = 0;
13
14         var t = new Date;
15         for ( i=0; i < LOOPS; i++ )
16                 arr.slice(0);
17         var tslice = new Date - t;
18         
19         t = new Date;
20         for ( i=0; i < LOOPS; i++ )
21                 arr.concat();
22         var tconcat = new Date - t;
23         
24         // clone() is just to see how fast built-ins are
25         t = new Date;
26         for ( i=0; i < LOOPS; i++ )
27                 clone(arr);
28         var tclone = new Date - t;
29         
30         alert([
31                 'slice:'+tslice,
32                 'concat:'+tconcat,
33                 'clone:'+tclone
34         ].join('\n'));
35         
36         
37         function clone(arr){
38                 var i = arr.length,
39                         copy = new Array(i);
40                         
41                 while (i--)
42                         copy[i] = arr[i];
43                         
44                 return copy;
45         }
46 })();
47 </script>