Is there any way to efficiently check if the variable is Object or Array, in NodeJS & V8?
I'm writing a Model for MongoDB and NodeJS, and to traverse the object tree I need to know if the object is simple (Number, String, ...) or composite (Hash, Array).
It seems that V8 has fast built-in Array.isArray
, but how to check if object is an Object? I mean complex object like hash {}
or instance of class, not something like new String()
?
Usually it may be done as this:
Object.prototype.toString.call(object) == "[object Object]"
or this:
object === Object(object)
But it seems that this operations aren't cheap, maybe there's some more efficient? It's ok if it's not universal and doesn't works on all engines, I need it only to work on V8.
x === Object(x)
but if you want more fine-grained testing, useObject.prototype.toString.call(x)
. Don't worry about the speed of either one, not without profiling it first. – Excellencetypeof x
is a poor choice? It will return 'string' for a string and 'object' for an object. – Lucielucientypeof []
returns"object"
– Eldred