forked from overset/javascript-natural-sort
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeed-tests.html
179 lines (161 loc) · 5.29 KB
/
speed-tests.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html>
<head>
<script src="naturalSort.js" type="text/javascript"></script>
<style>
* { font-family: tahoma; }
textarea{ width: 200px; }
.true { color: green; }
.false { color: red; }
</style>
</head>
<body style="display:none;"></body>
<script>
/*
* Alphanum
* http://my.opera.com/GreyWyvern/blog/show.dml/1671288
*/
function alphanum(a, b) {
function chunkify(t) {
var tz = [], x = 0, y = -1, n = 0, i, j;
while (i = (j = t.charAt(x++)).charCodeAt(0)) {
var m = (i == 46 || (i >=48 && i <= 57));
if (m !== n) {
tz[++y] = "";
n = m;
}
tz[y] += j;
}
return tz;
}
var aa = chunkify(a);
var bb = chunkify(b);
for (x = 0; aa[x] && bb[x]; x++) {
if (aa[x] !== bb[x]) {
var c = Number(aa[x]), d = Number(bb[x]);
if (c == aa[x] && d == bb[x]) {
return c - d;
} else return (aa[x] > bb[x]) ? 1 : -1;
}
}
return aa.length - bb.length;
}
/* Mike Grier's fixes on v0.2 http://mgrier.com/code/natsort.optimized.txt */
function naturalSort2(a,b){
// setup temp-scope variables for comparison evauluation
var x = a.toString().toLowerCase() || '',
re=/(-?[0-9.]+)/g,
y = b.toString().toLowerCase() || '',
nC = String.fromCharCode(0),
xN = x.replace( re, nC + '$1' + nC ).split(nC),
yN = y.replace( re, nC + '$1' + nC ).split(nC),
xD = (new Date(x)).getTime(),yD;
if(xD) yD = (new Date(y)).getTime(); //no point in getting yD if xD is not a date
// natural sorting of dates
if(yD){ // we already checked if(xD), so if(yD), it's a date, too
if( xD < yD ) return -1;
else if( xD > yD ) return 1;
}
// natural sorting through split numeric strings and default strings
var cLoc, numS=Math.max(xN.length,yN.length);
for(cLoc=0; cLoc<numS; cLoc++){
// instead of performing these next 6 operations in the if
// and the same 6 operations in the else if, just do them once
// so we can reuse results instead of computing twice
xNcL = xN[cLoc]; // only look up values
yNcL = yN[cLoc]; // in arrays once
FxNcL = parseFloat(xNcL);
FyNcL = parseFloat(yNcL);
oFxNcL = FxNcL || xNcL;
oFyNcL = FyNcL || yNcL;
if(oFxNcL < oFyNcL)return -1;
else if(oFxNcL > oFyNcL)return 1;
}
return 0;
}
/*
* Natural Sort algorithm for Javascript
* Version 0.3
* Author: Jim Palmer (based on chunking idea from Dave Koelle)
* optimizations and safari fix by Mike Grier (mgrier.com)
* Released under MIT license.
*/
function naturalSort3(a, b){
// setup temp-scope variables for comparison evauluation
var re = /(-?[0-9\.]+)/g,
x = a.toString().toLowerCase() || '',
y = b.toString().toLowerCase() || '',
nC = String.fromCharCode(0),
xN = x.replace( re, nC + '$1' + nC ).split(nC),
yN = y.replace( re, nC + '$1' + nC ).split(nC),
xD = (new Date(x)).getTime(),
yD = xD ? (new Date(y)).getTime() : null;
// natural sorting of dates
if ( yD )
if ( xD < yD ) return -1;
else if ( xD > yD ) return 1;
// natural sorting through split numeric strings and default strings
for( var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++ ) {
oFxNcL = parseFloat(xN[cLoc]) || xN[cLoc];
oFyNcL = parseFloat(yN[cLoc]) || yN[cLoc];
if (oFxNcL < oFyNcL) return -1;
else if (oFxNcL > oFyNcL) return 1;
}
return 0;
}
var t, diff, a = [], b = [], c = [], d = [];
for ( var i = 0; i < 1000; i++ ) {
t = String.fromCharCode( Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65, Math.floor(Math.random() * (90-66)) + 65 );
a.push(t);
b.push(t);
c.push(t);
d.push(t);
}
for ( i = 0; i < 1000; i++ ) {
t = Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString() + Math.floor(Math.random() * 9).toString();
a.push(t);
b.push(t);
c.push(t);
d.push(t);
}
for ( i = 0; i < 1000; i++ ) {
t = (Math.floor(Math.random() * 12) + 1).toString() + '/' + (Math.floor(Math.random() * 28) + 1).toString() + '/' + ( 2009 - Math.floor(Math.random() * 2));
a.push(t);
b.push(t);
c.push(t);
d.push(t);
}
function dateStr( diff ) {
return ', Time = [' + ((new Date()).getTime() - diff.getTime()) + 'ms]';
};
function matchingResults( str, z, suffix ) {
var i, diff = a.join(' ') === z.join(' ');
str += '<br>matches naturalSort result: <span class="' + diff + '">' + diff + '</span><br>';
if (!diff) {
str += '<textarea>Showing first 500 elements:\n';
// only show first 500 elements
for ( i = 0; i < 500; i++ ) {
str += a[i] + ( a[i] === z[i] ? ' = ' : ' \u2260 ' ) + z[i] + '\n';
}
str += '</textarea>';
}
document.write( str + ( suffix || '' ) );
};
diff = new Date();
document.write(
'<h1>Speed Tests</h1>All natural sorting algorithms are using the same data set' +
'<h2>naturalSort</h2>first element = ' + a.sort(naturalSort)[0] + dateStr( diff )
);
diff = new Date();
matchingResults(
'<h2>alphanum</h2>first element = ' + b.sort(alphanum)[0] + dateStr( diff ),
b,
'<br>*Note: alphanum does not sort dates'
);
diff = new Date();
matchingResults( '<h2>naturalSort2</h2>first element = ' + c.sort(naturalSort2)[0] + dateStr( diff ), c );
diff = new Date();
matchingResults( '<h2>naturalSort3</h2>first element = ' + d.sort(naturalSort3)[0] + dateStr( diff ), d );
document.getElementsByTagName('body')[0].style.display = '';
</script>
</html>