40

I just noticed the API docs for Node.js v0.10.26 provide for util.isArray,

util.isArray(object)# Returns true if the given "object" is an Array.

false otherwise.

var util = require('util');
util.isArray([])
  // true
util.isArray(new Array)
  // true
util.isArray({})
  // false

But, how is that different ecmascripts normal, Array.isArray?

> Array.isArray([]);
true
> Array.isArray(new Array);
true
> Array.isArray({});
false

4 Answers 4

70

To actually answer why util.isArray exists, we need a bit of a history lesson.

When it was first added to node, it did a bit more than call Array.isArray.

function isArray (ar) {
  return ar instanceof Array
      || Array.isArray(ar)
      || (ar && ar !== Object.prototype && isArray(ar.__proto__));
}

This was a local function in utils and actually wasn't exported until v0.6.0.

In this form, util.isArray handled a case that Array.isArray doesn't:

> x = [1,2,3]
[ 1, 2, 3 ]
> y = Object.create(x)
[ , ,  ]
> Array.isArray(y)
false
> Array.isArray(Object.getPrototypeOf(y))
true

There's some discussion here about this behavior of util.isArray, and consensus was that this behavior is actually bad because y is not really an Array.

Thus, the prototype-checking functionality was soon removed and replaced with a check that used both Array.isArray and a check of the argument's [[Class]].

function isArray(ar) {
  return Array.isArray(ar) ||
         (typeof ar === 'object' && objectToString(ar) === '[object Array]');
}

However, checking the [[Class]] is actually duplicate effort because Array.isArray also checks the [[Class]], so it too was eventually removed – leaving only a call to Array.isArray.

Today, util.isArray is just an alias of Array.isArray.

So in other words, the existence of util.isArray is mostly a legacy thing and can be safely ignored.

Sign up to request clarification or add additional context in comments.

6 Comments

Where can I read about the differences in y=Object.create(x) and x as far as the user is concerned? That thread linked shows the member functions being different. Also, in the first implimentation of when util.isArray was added wouldn't it have failed that test because util.isArray(y)'s call to Array.isArray(y) would have failed?
Object.create instantiates a new object and sets its prototype to the object passed as the first argument. It's a way to achieve classical inheritance or to instantiate an object without invoking its constructor. More info here and here.
...and no, that first implementation of isArray used the short-circuiting OR operator (||). So if the argument was instanceof Array, it would return true. If not, it would then call Array.isArray and return if true. If not, then it would do the prototype check and return true or false.
Lol, I see my bad overlooked that too quick.
+1 extra winnage for digging down into Array.isArray's implementation. I wish SO had more of this kind of answer! (Amazing I have to work my way around a plus one. I wish SO had less of this kind of fascist bizarro content control).
|
7

While the Node.js source uses them both, util.isArray uses Array.isArray internally (source)

In util.js:

function isArray(ar) {
  return Array.isArray(ar);
}
exports.isArray = isArray;

Comments

3

@SomeKittens was right, but that's a week ago. I patched node.js core and the docs. Now they're both the same thing, or will be in due time.

> Array.isArray === util.isArray;
true

Comments

0

It's for consistency reasons.

node.js has util.isFunction(), util.isObject(), util.isArray() and a number of similar functions. This way, type checks will all look similar to each other, instead of using different looking code each time.

4 Comments

This is speculative and incorrect. Array.isArray is mostly used throughout node core, although there are a few util.isArray calls scattered throughout as well (so much for consistency). util.isArray was originally exported as a user convenience, not because it's something used in other core modules.
inside of util.js only isArray is used
There's only one single call to isArray inside of util, and it's older than any of the developments made since util's isArray was introduced. If that line of code were written today, it would just call Array.isArray.
util is deprecated as of now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.