jjrv
jjrv

Reputation: 4345

Distinguish between number and object quickly

I've got a complex data structure in JavaScript composed out of numbers and containers (arrays in this case). What would be the fastest and/or most memory efficient way to store and process this? Is there something non-obvious, better speed and/or memory-wise, than for example [ [ 1, 2], [3, 4] ] which requires something similar to typeof to distinguish between tree branches and leaves when scanning it? The structure is very large with about a million numbers.

I've set up a jsperf test to evaluate some methods of distinguishing between numbers and objects, and typeof seems fastest except in Opera and IE:

http://jsperf.com/typeof-number-vs-object

Upvotes: 3

Views: 85

Answers (1)

Cerbrus
Cerbrus

Reputation: 72857

Try:

isNaN(myVar)

returns true id myVar is not a number, false otherwise.

Docs

Although, apparently, this function isn't entirely bug-free... (See the docs I linked to)

Upvotes: 1

Related Questions