forked from jquery/jquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
speed: committing a failing experiment of concat vs slice
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<script type="text/javascript"> | ||
(function(){ | ||
// Conclusion: slice is a little faster except on Chrome | ||
// clone() is slower on FF & IE but takes 50% on Safari & Chrome | ||
|
||
var SIZE = 1e4, | ||
LOOPS = 500; | ||
|
||
var arr = new Array(SIZE); | ||
for ( var i=arr.length-1; i >= 0; --i ) | ||
arr[i] = 0; | ||
|
||
var t = new Date; | ||
for ( i=0; i < LOOPS; i++ ) | ||
arr.slice(0); | ||
var tslice = new Date - t; | ||
|
||
t = new Date; | ||
for ( i=0; i < LOOPS; i++ ) | ||
arr.concat(); | ||
var tconcat = new Date - t; | ||
|
||
// clone() is just to see how fast built-ins are | ||
t = new Date; | ||
for ( i=0; i < LOOPS; i++ ) | ||
clone(arr); | ||
var tclone = new Date - t; | ||
|
||
alert([ | ||
'slice:'+tslice, | ||
'concat:'+tconcat, | ||
'clone:'+tclone | ||
].join('\n')); | ||
|
||
|
||
function clone(arr){ | ||
var i = arr.length, | ||
copy = new Array(i); | ||
|
||
while (i--) | ||
copy[i] = arr[i]; | ||
|
||
return copy; | ||
} | ||
})(); | ||
</script> |