Is number comparison faster than string comparison?

前端 未结 5 1777
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 02:36

I heard that hashing (ie converting a string or object to a number) is used for strings and such because it is easier to compare numbers than strings. If true, what is the reaso

5条回答
  •  星月不相逢
    2021-02-04 03:21

    Yes, but that has nothing to do with hashing.

    Comparing numbers involves simple hardware instructions that compare bits.

    Comparing strings involves (a) iterating through both strings until you find differing characters (unlike numbers, which are fixed-size) and (b) lots of Unicode magic (different strings of different lengths can actually be equal, and different characters in different code blocks compare differently).


    Hashing is typically used to convert a string into an array index.

提交回复
热议问题