Should jQuery use parseFloat (el.width ()) or parseInt (el.width (), 10)? which is more preferred

I always had this question:
When do I mind a floating number

Which one is preferable?

parseFloat

    someValue  = parseFloat(el.outerWidth())+parseFloat(ele2.css("marginRight")),

ParseInt

    someValue  = parseInt(el.outerWidth(), 10)+parseInt(ele2.css("marginRight"), 10),

Which method is easier for the JS engine?

+3
source share
3 answers

It is as wide as it has long been. parseFloathere it’s pointless because the values ​​will always be integers. I would rather store the bytes and use the unary operator+ :

someValue  = (+el.outerWidth())+(+ele2.css("marginRight"));
+3
source

: el.outerWidth() jQuery , . . .

, outerWidth(bool), , , , :

someValue = el.outerWidth(true);
+3

, , Andy E, : , parseFloat , , parseInt.

The size of the variable is an important factor in comparing performance, but it intalso floatoccupies the same memory space (4 bytes), so it really does not matter. Also parseFloat, it seems to do more computation and string analysis than parseInt.

+3
source

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


All Articles