4

So today I encountered this scenario. I had an integer and a string I needed to compare. In order to compare the two I would either have to toString() the integer, or parse the string to an int.

Here's my question, which one should I go for, is there any difference in performance for the two? (even if it is minimal) Is there a rule of thumb?

Here's a code example:

var intI = 1;
var stringS = '1';

if (intI.toString() == stringS)
console.log('are equal');

//Or
if (intI == parseInt(stringS))
console.log('are equal');

It would be best if I could declare the Integer as a string I know (as it is not used for calculations). But it is used everywhere on the site.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • Make sure when you call parseInt, you are setting the base, otherwise if your string is '07', it will be interpreted as octal – Dan McClain Dec 18 '12 at 13:29
  • 1
    beware of the second parameter of parseInt($num, 10). Do you want to parse the Integer as decimal, hexadecimal, octal or what?. For decimal use '10' as value for second parameter. That's not the default. – algorhythm Dec 18 '12 at 13:30
  • [related comment here](http://stackoverflow.com/questions/13907153/javascript-form-numbers/13907191#comment19166295_13907191) – rlemon Dec 18 '12 at 13:30

5 Answers5

12

It depends on the semantics you want more than performance. Should the string "001" be equal to the numeric value 1? If the answer is "yes", you should convert the values to numbers. If not, then you should compare as strings.

Don't worry about performance of such trivial things unless you've got an application that's doing millions of such operations.

Also note that parseInt() does not care whether a string of digits has trailing garbage at the end. That is

parseInt("123skidoo", 10) === 123

is true.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • hehe that's a whole new dimension. However in my case there will never be 0's in front of the string. Very valid point however. – Peter Rasmussen Dec 18 '12 at 13:30
1

is there any difference in performance for the two?

Not noticeable. However, there is a behavioral difference between the two methods, as parseInt would also convert strings such as 001 or 1xyz to the number 1. Do you want that? If not, you should toString the integer.

Btw, if you just use the == operator, the string will be ToNumbered implicitly. This does not allow strings that only begin with some digits (as 1xyz), but any number representation that is equivalent to 1 so for example 1.0 or 001.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Actually, you don't have to convert anything in this case. When you compare an integer to a string, the Javascript engine will convert one to the other automatically. You can try it by entering javascript:alert(1 == '1'); in your web browsers navigation bar. It will return "true".

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • [Didn't feel a need to dupe the comment](http://stackoverflow.com/questions/13934027/tostring-or-to-parseint-javascript#comment19426605_13934113) – rlemon Dec 27 '12 at 15:36
0

You do not need to do any explicit conversion as javascript handles it for you by default. Also it depends on semantics that you want to compare. I dont think we should worry about performance in such trivial cases.

LPD
  • 2,833
  • 2
  • 29
  • 48
  • This is not 100% true. `"1" == 1; // true` but `"1" === 1; // false` When comparing two different types you do need to convert them to be the same type to be 100% sure they are even. [read here](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use) – rlemon Dec 27 '12 at 15:35
  • @rlemon I think you have actually just proven that it works as expected here because it was doing a literal comparison not a type comparison it should be true when using `==` because `'1'` does `==` `1`. – Sammaye Dec 27 '12 at 17:05
  • yes it does, however it is not implicit with the semantics (i.e it may become confusing to later devs). The OP should convert the types and use exactly equal too. – rlemon Dec 27 '12 at 17:33
  • @rlemon I think that is kinda making a lot of new devs look bad, if some one doesn't know the difference between `==` and `===` then they have a serious problem especially if they think they can program javascript and should not be let near any such system. I mean am not saying you shouldn't cast but at the same time... – Sammaye Dec 27 '12 at 23:13
0

Try this

var intI = 1;
var stringS = '1';

to convert intI to string just use the below.

var intI = 1+"";//now check the type of intI, it would be string
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
kamal
  • 149
  • 6