1
0

slice.vs.concat.html 867 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. var SIZE = 1e4,
  7. LOOPS = 500;
  8. var arr = new Array(SIZE);
  9. for ( var i = arr.length - 1; i >= 0; --i )
  10. arr[i] = 0;
  11. var t = new Date;
  12. for ( i = 0; i < LOOPS; i++ )
  13. arr.slice(0);
  14. var tslice = new Date - t;
  15. t = new Date;
  16. for ( i = 0; i < LOOPS; i++ )
  17. arr.concat();
  18. var tconcat = new Date - t;
  19. // clone() is just to see how fast built-ins are
  20. t = new Date;
  21. for ( i = 0; i < LOOPS; i++ )
  22. clone(arr);
  23. var tclone = new Date - t;
  24. alert([
  25. 'slice:'+tslice,
  26. 'concat:'+tconcat,
  27. 'clone:'+tclone
  28. ].join('\n'));
  29. function clone(arr){
  30. var i = arr.length,
  31. copy = new Array(i);
  32. while (i--)
  33. copy[i] = arr[i];
  34. return copy;
  35. }
  36. })();
  37. </script>