2009-07-24 00:57:27 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
(function(){
|
2009-07-24 00:58:24 +00:00
|
|
|
// Conclusions:
|
|
|
|
// slice() is a little faster than concat() except on Chrome
|
|
|
|
// This clone() is slower on FF & IE but takes 50% on Safari & Chrome
|
2009-07-24 00:57:27 +00:00
|
|
|
|
|
|
|
var SIZE = 1e4,
|
|
|
|
LOOPS = 500;
|
2013-03-13 01:06:25 +00:00
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
var arr = new Array(SIZE);
|
2013-03-13 01:06:25 +00:00
|
|
|
for ( var i = arr.length - 1; i >= 0; --i )
|
2009-07-24 00:57:27 +00:00
|
|
|
arr[i] = 0;
|
|
|
|
|
|
|
|
var t = new Date;
|
2013-03-13 01:06:25 +00:00
|
|
|
for ( i = 0; i < LOOPS; i++ )
|
2009-07-24 00:57:27 +00:00
|
|
|
arr.slice(0);
|
|
|
|
var tslice = new Date - t;
|
2013-03-13 01:06:25 +00:00
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
t = new Date;
|
2013-03-13 01:06:25 +00:00
|
|
|
for ( i = 0; i < LOOPS; i++ )
|
2009-07-24 00:57:27 +00:00
|
|
|
arr.concat();
|
|
|
|
var tconcat = new Date - t;
|
2013-03-13 01:06:25 +00:00
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
// clone() is just to see how fast built-ins are
|
|
|
|
t = new Date;
|
2013-03-13 01:06:25 +00:00
|
|
|
for ( i = 0; i < LOOPS; i++ )
|
2009-07-24 00:57:27 +00:00
|
|
|
clone(arr);
|
|
|
|
var tclone = new Date - t;
|
2013-03-13 01:06:25 +00:00
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
alert([
|
|
|
|
'slice:'+tslice,
|
|
|
|
'concat:'+tconcat,
|
|
|
|
'clone:'+tclone
|
|
|
|
].join('\n'));
|
2013-03-13 01:06:25 +00:00
|
|
|
|
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
function clone(arr){
|
|
|
|
var i = arr.length,
|
|
|
|
copy = new Array(i);
|
2013-03-13 01:06:25 +00:00
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
while (i--)
|
|
|
|
copy[i] = arr[i];
|
2013-03-13 01:06:25 +00:00
|
|
|
|
2009-07-24 00:57:27 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
})();
|
2013-03-13 01:06:25 +00:00
|
|
|
</script>
|