@@ -5,6 +5,7 @@ export class SearchPerformanceTest extends Component {
5
5
state = {
6
6
needle : '' ,
7
7
haystack : [ ] ,
8
+ haystackConvertedToNumbers : [ ] ,
8
9
lastResultSet : null ,
9
10
}
10
11
@@ -19,39 +20,73 @@ export class SearchPerformanceTest extends Component {
19
20
// Need a sorted array for binary search
20
21
if ( searchMethodName === 'Binary Search' ) {
21
22
for ( let i = 0 ; i < 100000 ; i += 5 ) {
22
- initialSearchArray . push ( String ( i + 1 + Math . floor ( Math . random ( ) * 4 ) ) )
23
+ initialSearchArray . push ( i + 1 + Math . floor ( Math . random ( ) * 4 ) )
23
24
}
24
25
// Array can be in random order for other search methods
25
26
} else {
26
27
for ( let i = 0 ; i < 20000 ; i ++ ) {
27
- initialSearchArray . push ( String ( Math . floor ( Math . random ( ) * 20000 ) ) )
28
+ initialSearchArray . push ( Math . floor ( Math . random ( ) * 20000 ) )
28
29
}
29
30
}
30
31
31
- this . setState ( { haystack : initialSearchArray } )
32
+ this . setState ( {
33
+ haystack : initialSearchArray ,
34
+ haystackConvertedToNumbers : initialSearchArray ,
35
+ } )
32
36
}
33
37
34
38
handleNeedleChange = e => {
35
39
this . setState ( { needle : e . target . value } )
36
40
}
37
41
38
42
handleHaystackChange = e => {
39
- this . setState ( { haystack : e . target . value . split ( ', ' ) } )
43
+ const updatedHaystack = e . target . value . split ( ', ' )
44
+ const haystackConvertedToNumbers = updatedHaystack . reduce (
45
+ ( fullArray , value ) => {
46
+ const valueWithoutTrailingComma = value . replace ( ',' , '' )
47
+ if ( valueWithoutTrailingComma === '' ) {
48
+ return fullArray
49
+ }
50
+
51
+ const valueConvertedToNumberIfPossible = isNaN (
52
+ valueWithoutTrailingComma
53
+ )
54
+ ? valueWithoutTrailingComma
55
+ : Number ( valueWithoutTrailingComma )
56
+ return [ ...fullArray , valueConvertedToNumberIfPossible ]
57
+ } ,
58
+ [ ]
59
+ )
60
+ this . setState ( { haystack : updatedHaystack , haystackConvertedToNumbers } )
40
61
}
41
62
42
63
runPerformanceTest = e => {
43
64
e . preventDefault ( )
44
65
45
- const { needle, haystack } = this . state
66
+ const { needle, haystackConvertedToNumbers } = this . state
46
67
const { searchMethod } = this . props
47
68
69
+ if (
70
+ ! needle ||
71
+ ! haystackConvertedToNumbers ||
72
+ haystackConvertedToNumbers . length === 0
73
+ ) {
74
+ return this . setState ( { lastResultSet : null } )
75
+ }
76
+
77
+ const needleConvertedToNumberIfPossible = isNaN ( needle )
78
+ ? needle
79
+ : Number ( needle )
48
80
const startTime = performance . now ( )
49
- const result = searchMethod ( haystack , needle )
81
+ const result = searchMethod (
82
+ haystackConvertedToNumbers ,
83
+ needleConvertedToNumberIfPossible
84
+ )
50
85
const endTime = performance . now ( )
51
86
52
87
this . setState ( {
53
88
lastResultSet : {
54
- haystack,
89
+ haystack : [ ... haystackConvertedToNumbers ] ,
55
90
needle,
56
91
result,
57
92
timeTaken : endTime - startTime ,
@@ -113,7 +148,7 @@ export class SearchPerformanceTest extends Component {
113
148
< div className = "arrayHaystack" >
114
149
< b > Array Haystack:</ b > { ' ' }
115
150
< div className = "arrayHaystackCharacters" >
116
- { haystack . join ( ', ' ) }
151
+ { lastResultSet . haystack . join ( ', ' ) }
117
152
</ div >
118
153
</ div >
119
154
</ div >
0 commit comments