String comparison and hashing

I recently learned about the hash data structure , and basically one of its main uses for finding a substring within a string. Here are some of the benefits I noticed:

  • Comparing two lines can be expensive, so this should be avoided if possible.
  • Hashing strings and comparing hashes is usually much faster than comparing strings, however re-permuting a new substring each time traditionally takes linear time
  • A compressed hash is capable of rebuilding a new substring in constant time, making it much faster and more efficient for this task.

I went ahead and implemented a hash style in JavaScript and started analyzing the speed between custom hash, traditional renaming and just comparing substrings with each other.

In my conclusions, the larger the substring, the longer it took the usual approach to rethinking (as expected), when the sliding hash ran incredibly fast (as expected). However, comparing substrings together was much faster than a rolling hash. How could this be?

From a perspective perspective, let's say that the runtime for functions running through a string of about 2.4 million characters for a 100-character substring was as follows:

  • Rolling Hash - 0.809 seconds
  • Traditional switchover - 71.009 seconds
  • Simple string comparison (no hash) 0.089 seconds

, ? , - JavaScript ? JavaScript; , ?

, / JavaScript, , .

: stringA === stringB

: Computer Science Community , , , , JavaScript.

+4
1

, , - , .



JavaScript , , , , , .

+2

Source: https://habr.com/ru/post/1624603/


All Articles