Distinguish between number and object quickly

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

I've set up a jsperf benchmark to evaluate some methods for distinguishing between numbers and objects, and typeof seems to be the fastest, except for Opera and IE:

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

+3


source to share


1 answer


Try:

isNaN(myVar)

      

returns true

id is myVar

not a number, but false otherwise.



Docs

Although apparently this feature is not completely crash-free (see the docs I linked to)

+1


source







All Articles