Here’s my solution without any split/map/reduce method, feedback appreciated for further optimization of code, is the current code much perform much worse without above mentioned methods?
function findLongestWordLength(str) {
let max = 0;
let i = 0;
while (str[i] && i < str.length){ //restrict to str length range
let count = 0;
while(str[i] && str[i] !== ' '){ //counting a valid word(defined by non space)
i++;
count++;
}
if (count > max){ //check if counted word exceed the current max, if yes replace
max = count;
}
while(str[i] && str[i] === ' '){ //skipping spaces
i++;
}
}
return max;
}
What do you mean by ‘perform much worse’?
You could make this code simpler by using those methods.
Sorry to clarify, by performance i meant shorter running time, not simplicity or length of code.
You might get marginally better performance with built in methods. Only way to know is to benchmark it.
I’m not sure the best way to benchmark it , here is one i did on
Except of Solution 5 being consistently the worst, others ranking are not that consistent, does performance normally mean average of ranking ?
result
---
Solution 2 (4447442) 🏆
**100%**
Solution 6 (single while loop solution,credits to @RandellDawson for optimizing it) (4430390)
**99.62%**
Solution 3 (4393303)
**98.78%**
Solution 4 (4383382)
**98.56%**
Solution 1 (4360333)
**98.04%**
Solution 7 (my nested while loop solution) (4143187)
**93.16%**
Solution 5 (513142)
**11.54%**