I found two search options in the string:

"http://www.youtube.com/embed/8rwsuXHA7RA".indexOf("youtube.com") != -1 

and

 ~"http://www.youtube.com/embed/8rwsuXHA7RA".indexOf("youtube.com") 

Judging by the test , the second option is faster, but please explain how exactly it works.

  • they do things differently ... In the first case, you get a Boolean, and in the second case you get a number. ~ " youtube.com/embed/… -12" youtube.com/embed/… ! = -1 true es5.javascript.ru/x11.html#x11.4.8 You can’t call indexOf without using it. - zb '
  • Yes, they work differently. The question of exactly how the second variant works - vox
  • well, the algorithm in the specification is described, it is not clear what is the point of comparing them, and well, it is clear ~-1 === 0 - zb '28
  • +1 even faster ... jsperf.com/parkovjkeee/8 - zb '28

1 answer 1

~ - Bitwise NOT. Proof
Converts the input number according to the formula: -(number + 1) .
Those. 42 at -43, 789 at -790, -123 at 122, etc.

  • one
    `-43```````` - zb '
  • @eicto, yes, it's an absolute mistake, I have never used this operator, for the rest is enough with my head. For now, anyway. - user31688