0

Usually when I have to parse number in javascript I use code like

var x="99"
var  xnumber= x-0

instead of

var xnumber= parseInt(x)

Is there any problem in using this Code ( in the performance or the structure ) and I want to know if there is any problem

Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
Suhail Keyjani
  • 488
  • 3
  • 10

4 Answers4

1

Both ways should work but it is in my opinion "cleaner" to parse any String with the function which is made for this and not with this little trick.

You are also less likely to run into conversion issues and should you write code in other languages you are in general better of with the way the developer intended to use. Many other languages will not allow your first way of "casting"

JRsz
  • 2,891
  • 4
  • 28
  • 44
  • I agree that both ways will work. And in general, I say "go for the clearer options". However, in this case, since there are significant performance benefits to using the x - 0 method, I think it should be used in cases where performance is a concern. – cutmancometh Nov 24 '15 at 00:01
  • I do not know of a huge performance gap. Do you have any benchmarks which show the difference? – JRsz Nov 24 '15 at 11:01
1

Using the "x - 0" method is going to be significantly faster in most browsers.

Here's a JSPerf that shows the performance difference.

You can do you own A/B performance testing using JSPerf.com

However, you may still want to use parseInt() in some cases, because it's a little clearer. Although, truthfully, any experienced javascript developer isn't going to have any trouble understanding the faster way.

If the line of code is only going to run once every half second or so (or whenever the user types a letter), you can use parseInt without worrying.

However, if this bit of code is in a loop that runs a few thousand times or more, you should definitely use x - 0.

cutmancometh
  • 1,677
  • 3
  • 20
  • 28
0

performance wise you wont see much of a difference, but it's not very readable. I tend to believe parseInt("4", 10) is the right solution because it's inline with how you parse other values in javascript (i.e. parseFloat("4.0")) so you're keeping more consistent by sticking with one method of parsing numbers.

David Zorychta
  • 13,039
  • 6
  • 45
  • 81
  • 1
    And do not forget to pass the base argument: `parseInt(string, 10)` (http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior) – Microfed Nov 22 '15 at 18:53
0

I don't believe it will perform better or worse. Performance optimisations like that one are generally negligible, and can be inconsistent across various platforms. However, the former method using parseInt gives a much better idea of your intentions to other programmers. Unless you have a very good reason to optimize this statement, and you are sure it really runs faster in your target environment, you are recommended to stick to the more readable version (this case, parseInt).

Update: I recommend reading this thread about micro-optimisations: https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding/

Community
  • 1
  • 1
David Rissato Cruz
  • 3,347
  • 2
  • 17
  • 17
  • Never said that a difference couldn't exist. I just tried to say that if this piece of code isn't a critical path to a possible bottlenec, it should be written in a manner where readability is favored – David Rissato Cruz Nov 23 '15 at 18:09
  • By the way, there is a really good thread about micro-optimisations in code that can be found over here: http://programmers.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding/ – David Rissato Cruz Nov 23 '15 at 23:17