javascript - String Comparison vs. Hashing -
i learned rolling hash data structure, , 1 of prime uses searching substring within string. here advantages noticed:
- comparing 2 strings can expensive should avoided if possible
- hashing strings , comparing hashes faster comparing strings, rehashing new substring each time traditionally takes linear time
- a rolling hash able rehash new substring in constant time, making quicker , more efficient task
i went ahead , implemented rolling hash in javascript , began analyze speed between rolling hash, traditional rehashing, , comparing substrings against each other.
in findings, larger substring, longer took traditional rehashing approach run (as expected) rolling hash ran incredibly fast (as expected). however, comparing substrings ran faster rolling hash. how be?
for sake of perspective, let's running times functions searching through ~2.4 million character string 100 character substring following:
- rolling hash - 0.809 seconds
- traditional rehashing - 71.009 seconds
- just comparing strings (no hashing) 0.089 seconds
how string comparing faster rolling hash? have javascript in particular? strings primitive type in javascript; cause string comparisons run in constant time?
my main confusion how/why string comparisons fast in javascript, when under impression supposed relatively slow.
note: string comparisons i'm referring stringa === stringb
note: asked question on over computer science community , informed should ask here because javascript specific.
after testing , analysis, i've come conclusion there few reasons why rolling hash approach running slower comparing 2 strings.
if rolling hash claims run in constant time, how can slower comparing strings?
functions relatively slow - calling function slower executing code inline. in particular case, function had called on object every time rolling hash rehashes internal window, therefore taking longer run compared string comparison, since code inline. since benchmark has rolling hash "shift" on 2 million iterations, function slow down can seen more clearly.
but why string comparison fast?
strings primitive - basically, because strings primitive type in javascript, attempting compare 2 strings invoke routine coded directly within interpreter. low level evaluation can done fast architecture possibly can (similar comparing numbers).
in conclusion
comparing strings in javascript end being faster rolling hash in scenario because strings primitive, therefore allowing interpreter work these elements quickly, , because calling functions create slight overhead , slow down process on small scale.
Comments
Post a Comment