speed-tests.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="naturalSort.js" type="text/javascript"></script>
  5. <script>
  6. /* Mike Grier's fixes on v0.2 http://mgrier.com/code/natsort.optimized.txt */
  7. function naturalSort2(a,b){
  8. // setup temp-scope variables for comparison evauluation
  9. var x = a.toString().toLowerCase() || '',
  10. re=/(-?[0-9.]+)/g,
  11. y = b.toString().toLowerCase() || '',
  12. nC = String.fromCharCode(0),
  13. xN = x.replace( re, nC + '$1' + nC ).split(nC),
  14. yN = y.replace( re, nC + '$1' + nC ).split(nC),
  15. xD = (new Date(x)).getTime(),yD;
  16. if(xD) yD = (new Date(y)).getTime(); //no point in getting yD if xD is not a date
  17. // natural sorting of dates
  18. if(yD){ // we already checked if(xD), so if(yD), it's a date, too
  19. if( xD < yD ) return -1;
  20. else if( xD > yD ) return 1;
  21. }
  22. // natural sorting through split numeric strings and default strings
  23. var cLoc, numS=Math.max(xN.length,yN.length);
  24. for(cLoc=0; cLoc<numS; cLoc++){
  25. // instead of performing these next 6 operations in the if
  26. // and the same 6 operations in the else if, just do them once
  27. // so we can reuse results instead of computing twice
  28. xNcL = xN[cLoc]; // only look up values
  29. yNcL = yN[cLoc]; // in arrays once
  30. FxNcL = parseFloat(xNcL);
  31. FyNcL = parseFloat(yNcL);
  32. oFxNcL = FxNcL || xNcL;
  33. oFyNcL = FyNcL || yNcL;
  34. if(oFxNcL < oFyNcL)return -1;
  35. else if(oFxNcL > oFyNcL)return 1;
  36. }
  37. return 0;
  38. }
  39. /*
  40. * Natural Sort algorithm for Javascript
  41. * Version 0.3
  42. * Author: Jim Palmer (based on chunking idea from Dave Koelle)
  43. * optimizations and safari fix by Mike Grier (mgrier.com)
  44. * Released under MIT license.
  45. */
  46. function naturalSort3(a, b){
  47. // setup temp-scope variables for comparison evauluation
  48. var re = /(-?[0-9\.]+)/g,
  49. x = a.toString().toLowerCase() || '',
  50. y = b.toString().toLowerCase() || '',
  51. nC = String.fromCharCode(0),
  52. xN = x.replace( re, nC + '$1' + nC ).split(nC),
  53. yN = y.replace( re, nC + '$1' + nC ).split(nC),
  54. xD = (new Date(x)).getTime(),
  55. yD = xD ? (new Date(y)).getTime() : null;
  56. // natural sorting of dates
  57. if ( yD )
  58. if ( xD < yD ) return -1;
  59. else if ( xD > yD ) return 1;
  60. // natural sorting through split numeric strings and default strings
  61. for( var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++ ) {
  62. oFxNcL = parseFloat(xN[cLoc]) || xN[cLoc];
  63. oFyNcL = parseFloat(yN[cLoc]) || yN[cLoc];
  64. if (oFxNcL < oFyNcL) return -1;
  65. else if (oFxNcL > oFyNcL) return 1;
  66. }
  67. return 0;
  68. }
  69. var a = [], b = [], c = [];
  70. for ( var i = 0; i < 1000; i++ ) {
  71. a.push(String.fromCharCode( Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65 ));
  72. b.push(String.fromCharCode( Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65 ));
  73. c.push(String.fromCharCode( Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65 ));
  74. }
  75. for ( i = 0; i < 1000; i++ ) {
  76. a.push(Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() );
  77. b.push(Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() );
  78. c.push(Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() );
  79. }
  80. for ( i = 0; i < 1000; i++ ) {
  81. a.push( (Math.floor(Math.random() * 12) + 1).toString() + '/' + (Math.floor(Math.random() * 28) + 1).toString() + '/' + ( 2009 - Math.floor(Math.random() * 2)) );
  82. b.push( (Math.floor(Math.random() * 12) + 1).toString() + '/' + (Math.floor(Math.random() * 28) + 1).toString() + '/' + ( 2009 - Math.floor(Math.random() * 2)) );
  83. c.push( (Math.floor(Math.random() * 12) + 1).toString() + '/' + (Math.floor(Math.random() * 28) + 1).toString() + '/' + ( 2009 - Math.floor(Math.random() * 2)) );
  84. }
  85. var d = new Date();
  86. document.write(a.sort(naturalSort)[0] + ' <BR>[' + ((new Date()).getTime() - d.getTime()) + 'ms]<BR><BR>');
  87. //d = new Date();
  88. //document.write(b.sort(naturalSort2)[0] + ' <BR>[' + ((new Date()).getTime() - d.getTime()) + 'ms]<BR><BR>');
  89. //d = new Date();
  90. //document.write(c.sort(naturalSort3)[0] + ' <BR>[' + ((new Date()).getTime() - d.getTime()) + 'ms]');
  91. </script>
  92. <style>*{font-family:tahoma;font-size:9px;}</style>
  93. </head>
  94. <body></body>
  95. </html>