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