Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently check if variable is Array or Object (in NodeJS & V8)?

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.

like image 732
Alex Craft Avatar asked Jan 12 '12 11:01

Alex Craft


People also ask

How do you check if a value is an array or object in node JS?

isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false .

How do you check if a variable is an object or array?

Answer: Use the Array. isArray() Method isArray() method to check whether an object (or a variable) is an array or not. This method returns true if the value is an array; otherwise returns false .

How do you check if a variable is array or not in node JS?

Method 1: Using the isArray method isArray() method checks whether the passed variable is an Array object. It returns a true boolean value if the variable is an array and false if it is not.

How do you check if an object is an array or not in JavaScript?

The isArray() method returns true if an object is an array, otherwise false .


2 Answers

For simply checking against Object or Array without additional function call (speed).

isArray()

let isArray = function(a) {     return (!!a) && (a.constructor === Array); }; console.log(isArray(        )); // false console.log(isArray(    null)); // false console.log(isArray(    true)); // false console.log(isArray(       1)); // false console.log(isArray(   'str')); // false console.log(isArray(      {})); // false console.log(isArray(new Date)); // false console.log(isArray(      [])); // true 

isObject()

let isObject = function(a) {     return (!!a) && (a.constructor === Object); }; console.log(isObject(        )); // false console.log(isObject(    null)); // false console.log(isObject(    true)); // false console.log(isObject(       1)); // false console.log(isObject(   'str')); // false console.log(isObject(      [])); // false console.log(isObject(new Date)); // false console.log(isObject(      {})); // true 
like image 54
zupa Avatar answered Oct 14 '22 14:10

zupa


All objects are instances of at least one class – Object – in ECMAScript. You can only differentiate between instances of built-in classes and normal objects using Object#toString. They all have the same level of complexity, for instance, whether they are created using {} or the new operator.

Object.prototype.toString.call(object) is your best bet to differentiate between normal objects and instances of other built-in classes, as object === Object(object) doesn't work here. However, I can't see a reason why you would need to do what you're doing, so perhaps if you share the use case I can offer a little more help.

like image 23
Andy E Avatar answered Oct 14 '22 15:10

Andy E