DEV Community

Discussion on: Clever coding tricks ( that we don't need )

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

It should be noted that ~~ is not the same as Math.floor - it simply removes the decimal part, and is consistently faster at doing so than Math.trunc - which is the normal suggested 'readable' way of doing so (at least in my tests - sometimes up to 50% faster):

Math.floor(2.1) // 2
~~ 2.1 // 2

Math.floor(-2.1) // -3
~~ -2.1 // -2
Enter fullscreen mode Exit fullscreen mode

It should also be remembered that performance is very important in some situations, and also that readability is purely subjective.

Collapse
 
dvddpl profile image
Davide de Paolis

absolutely, very important in some situations, completely irrelevant in many others.
and also agree on the subjectivity of readability. but still prefer to read Math.floor than ~~