2

I need to parse querystrings that contain both text and numbers. For example the following querystring:

?userID=12&team=Sales&quarter=Q1&count=2310

should be translated into the following JavaScript object:

{
userID:12, // not "12"
team:"Sales",
quarter:"Q1",
count:2310 // not "2310"
}

Currently I am doing it in two steps:

  1. Parse the querystring
  2. Go through all the parameters and identify which ones are numbers (either with a regex or an isNumber function !isNaN(parseFloat(n)) && isFinite(n)

This seems rather inefficient especially as most of my parameters are not numbers. Is there a better way?

Christophe
  • 27,383
  • 28
  • 97
  • 140
  • Not sure if this applies to javascript as well as C#, but ... [related question](http://stackoverflow.com/questions/7461080/fastest-way-to-check-if-string-contains-only-digits). – jbabey Feb 22 '13 at 20:52
  • @jbabey thx, but it doesn't seem to work well in my case as I might have decimal numbers. – Christophe Feb 22 '13 at 20:58

3 Answers3

1

Two suggestions:

  1. If you know which parameters are going to hold numbers, only do the conversion for those
  2. The fastest way to convert strings to numbers as far as I know is to use the unary operator on them, as follows:

    +(strVar)

Also multiplying by 1 is supposed to be fast AFAIK

Dexygen
  • 12,287
  • 13
  • 80
  • 147
  • I collect parameters from many forms and don't have a list of those that hold numbers. Thanks for the idea of the unary operator, what would it do on non-numbers? – Christophe Feb 22 '13 at 20:53
  • not number multiplying by 1 return NAN you could make a quaternary like var num = (isNAN(o.count*1)) ? o.count : o.count*1 – ncubica Feb 22 '13 at 21:00
  • Just did a test with +(string) :-) – Christophe Feb 22 '13 at 21:14
1

do you know where are you going to use the specify value?

Because if you multiplying any string in number format like "3239" by 1 this will convert that string in number..

var example = 5 + (o.count*1) //o.count will be a number...
ncubica
  • 8,169
  • 9
  • 54
  • 72
0

After you parse the querystring you can convert those string representations of integer value to an actual integer like this:

var obj; // your object that the string is parsed into, with all values as strings.
for (var prop in obj) {
    if (String(parseInt(obj[prop])) === obj[prop]) {
        obj[prop] = parseInt(obj[prop]);
    }
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Right, this is kind of what I am doing now with the isNumber function. I was looking for a more efficient method, either identify numbers while parsing or not having to test all parameters. – Christophe Feb 22 '13 at 20:56
  • 1
    @Christophe I guess the approach would depend on whether you knew what all the properties names were going to be ahead of time, and understand which of these will be expected to be integers. If you want handle arbitrary properties, I don't think you will be able to avoid doing such a number check on all properties. This is probably a micro-optimization anyway unless you are going to be doing this operation an really large query strings and/or doing it a large number of times. – Mike Brant Feb 22 '13 at 21:08
  • You could improve the above by storing the result of parseInt(obj[prop]) in a variable, so that you don't call parseInt twice – Dexygen Feb 22 '13 at 21:53