javascript - In jQuery, should i use parseFloat(el.width()) or parseInt(el.width(),10)? which one is more preferred - Stack Over

I always had this question: When i dont mind the exact floating number Which one is preferred?parseFlo

I always had this question:
When i dont mind the exact floating number

Which one is preferred?

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?

I always had this question:
When i dont mind the exact floating number

Which one is preferred?

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?

Share Improve this question edited Mar 11, 2010 at 19:18 adardesign asked Mar 11, 2010 at 15:22 adardesignadardesign 35.9k15 gold badges66 silver badges86 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

It's as broad as it's long really. parseFloat is pointless here because the values will always be integers. I'd rather save on bytes and use the unary operator +:

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

When you're doing: el.outerWidth() jQuery is already returning and integer, see the docs for return types. So in this case, there's no need to parse the width at all.

It should be noted, there's another overload of outerWidth(bool) that includes the margin if you want left and right margins, you can just do this if that's the case:

someValue = el.outerWidth(true);

The best solution is of course Andy E's solution, but to answer your question: I think parseFloat is pointless if your number have not a floating-point, so I would use parseInt.

The size of the variable is an important factor in those performance parisons, but int and float take up the same space in the memory (4 bytes), so it dosen't really matter. In addition, parseFloat seems to do more calculating and string-parsing than parseInt.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744380222a4571390.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信